find and solve || findandsolve.com
Please wait.....

C# String ToString()

In the c# ToString() method converts the value of this instance to a String.ToString() method is inherited from the Object class which is used to get a string.

Syntax

public override string ToString()
public string ToString(IFormatProvider) 

Parameter

It does not any parameter.

Return

This method returns a String representation of the collection

Example

In the given below programming code, GetType() method is used to get the type of the current object. It will clarify whether the given Stack object is converted into the string or not.

using System;
public class Program
{
 public static void Main()
 {
           string str = "Hello Find And Solve";
           int intA= 98;
           string str1 = str.ToString();
           string str2 = intA.ToString();
           Console.WriteLine(str1);
           Console.WriteLine(str2);
    }
}

Output

Hello Find And Solve

98

Overriding ToString() method to give a same message for every object.

using System;
public class Program
{
 public static void Main()
 {
    Program objFirst = new Program();
    Program objSecond = new Program();
  //Printing two objects of class A
Console.WriteLine(objFirst);
 Console.WriteLine(objSecond);
 }
 //Overriding the ToString() method
public override String ToString()
{   return "We have created an object"; }
}

Output

We have created an object

We have created an object