By 22/07/2015 1 Comments

C# ToString Method

  • C# ToString method is a virtual method which has string as its return type, it has been declared in the object class.
  • When we create or declare our custom structures, classes or new types, these structures classes or custom types inherit from the object class implicitly in .NET framework, it means ToString() method is available in our custom structures, types or classes, notice ToString() method is marked with a keyword (virtual) in object class which means it can be overridden in our structures, custom types or classes, or another words the ToString() method can be re-implemented with our own logic in our custom classes/types through override it.
  • The job of ToString() method is: it converts an object into string form and returns that object’s information in string representation as it can be easily displayed and becomes human readable.
  • In .NET framework the object class is a father of all data types and custom types/classes.


using System;
namespace csharpBasic
    class Technology

  // Instance fields are declared.
        string Html5;
        string Jscript;

  // Following is a constructor declaration with 2 string parameters.
        public Technology(string html5, string jscript)
            this.Html5 = html5;
            this.Jscript = jscript;

   /* Following is declaration of a string type ToString() method 
      which is override in this class, notice this method 
      is automatically called by object of this class. */

        public override string ToString()

   /* Following statement returns an object information 
      by Format() method.*/
            return string.Format("{0} - {1}", this.Html5, this.Jscript);

    class Program

   // Static main method void type declaration.
        static void Main(string[] args)

  // An object creation with constructor that passes 2 arguments in it.
            Technology technology = new Technology("Html5", "Javascript");
            Console.WriteLine("Technology object information:");

  /* Following statement displays the string represented object 
     information by printing reference variable (technology),
     note: at this stage the ToString() method is being 
     called automatically. */

            Console.WriteLine("\t {0}", technology);
        The Output will be:
        Technology object information:
                 Html5 - Javascript

Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .