abubakkar
abubakkar

Reputation: 187

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();
    }
  }
}

Upvotes: 9

Views: 3946

Answers (7)

user63762453
user63762453

Reputation: 1734

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.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201399

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.

Upvotes: 9

Rangesh
Rangesh

Reputation: 728

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.

Upvotes: 3

jungNitesh
jungNitesh

Reputation: 39

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.

Upvotes: 1

user2920518
user2920518

Reputation: 318

.NET is calling the ToString method implicitly when you pass the object to WriteLine.

Upvotes: 1

Ahmad Mageed
Ahmad Mageed

Reputation: 96477

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.

Upvotes: 6

Sajeetharan
Sajeetharan

Reputation: 222522

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());

Upvotes: 3

Related Questions