简体   繁体   中英

What is the need of ToString() in C#?

I'm using the below code in c sharp. But both the WriteLine statements are giving the same result 25. Then what is the need for converting Tostring in c sharp? Is there any special purpose?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sample
{
  class Program
  {
    static void Main(string[] args)
    {
        int value = 25;
        Console.WriteLine(value.ToString());
        Console.WriteLine(value);
        Console.ReadLine();
    }
  }
}

If I understand your question, the purpose of ToString() is to provide a consistent mechanism to convert objects to strings. In your Console.WriteLine(value); example you are using an Overloaded version of WriteLine that takes an int , but in the general case of using an object the system needs a mechanism of providing a textual representation.

You should override ToString in a class to give it a desired string representation. The Console.WriteLine method calls ToString , per MSDN :

Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream.

Take this class for example:

public class Person 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now try to display it:

Console.WriteLine(new Person { Id = 42, Name = "Ahmad" });

The class doesn't override ToString , so the output is something like: MyNamespace.Program+Person

Now, let's override ToString to display a friendlier string representation:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public override string ToString()
    {
        return String.Format("{0} - {1}", Id, Name);
    }
}

Now the Console.WriteLine returns 42 - Ahmad .

The ToString() of any object is supposed to return a string representation of that object. In your Code,

Int32, it returns a string correspoding to the object (value of the integer).

int value = 25;
Console.WriteLine(value.ToString());

Adding A bit more Information,If you decompile Your Program you can find that

Console.WriteLine(value.ToString())

The above line would call

/**Methods are from mscorlib.dll**/
    public virtual void Write(string value)
            {
                if (value != null)
                {
                    this.Write(value.ToCharArray());
                }
            }

While,

Console.WriteLine(value);

The above line would call

   public virtual void Write(int value)
    {
        this.Write(value.ToString(this.FormatProvider));
    }

If you are curious to know,how the Cast is made(ToString),then this might help you.

As you might know that Tostring() Returns a string that represents the current object. You are right both lines (with and without Tostring() ) would return same result because even if you don't use Tostring() there will be an implicit call to it since Console.writeline() prints a String representation.

Read more about this here.

当您将对象传递给WriteLine时,.NET会隐式调用ToString方法。

In programming languages ToString() or its equivalent is used to represent any object textually. Or simply it can be said that it is just used to convert any object to plain string format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM