简体   繁体   English

C#中ToString()的需求是什么?

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

I'm using the below code in c sharp. 我在c sharp中使用下面的代码。 But both the WriteLine statements are giving the same result 25. Then what is the need for converting Tostring in c sharp? 但是两个WriteLine语句都给出了相同的结果25.那么在c sharp中转换Tostring需要什么? 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. 如果我理解你的问题, ToString()的目的是提供一个将对象转换为字符串的一致机制。 In your Console.WriteLine(value); 在您的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. 例如,您正在使用带有intWriteLine的重载版本,但在使用object的一般情况下,系统需要提供文本表示的机制。

You should override ToString in a class to give it a desired string representation. 您应该在类中重写ToString以为其提供所需的字符串表示形式。 The Console.WriteLine method calls ToString , per MSDN : Console.WriteLine方法根据MSDN调用ToString

Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream. 否则,调用值的ToString方法以生成其字符串表示形式,并将结果字符串写入标准输出流。

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 该类不会覆盖ToString ,因此输出类似于: MyNamespace.Program+Person

Now, let's override ToString to display a friendlier string representation: 现在,让我们重写ToString以显示更友好的字符串表示:

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 . 现在Console.WriteLine返回42 - Ahmad

The ToString() of any object is supposed to return a string representation of that object. 任何对象的ToString()都应该返回该对象的字符串表示。 In your Code, 在您的代码中,

Int32, it returns a string correspoding to the object (value of the integer). Int32,它返回一个字符串对应的对象(整数的值)。

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); Console.WriteLine(值);

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. 如果您想知道Cast是如何制作的(ToString),那么这可能会对您有所帮助。

As you might know that Tostring() Returns a string that represents the current object. 您可能知道Tostring()返回表示当前对象的字符串。 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. 你是对的(有和没有Tostring() )会返回相同的结果,因为即使你不使用Tostring() ,也会有一个隐式调用,因为Console.writeline()打印一个String表示。

Read more about this here. 在这里阅读更多相关信息

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

In programming languages ToString() or its equivalent is used to represent any object textually. 在编程语言中,ToString()或其等价物用于以文本方式表示任何对象。 Or simply it can be said that it is just used to convert any object to plain string format. 或者简单地说,它只是用于将任何对象转换为纯字符串格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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