为什么object.ToString()存在吗? [英] Why does object.ToString() exist?

查看:263
本文介绍了为什么object.ToString()存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是更优雅,整洁有一个 IStringable 接口?

Isn't it much more elegant and neat to have an IStringable interface?

谁需要这个 Type.FullName 返回给我们的对象?

Who needs this Type.FullName object returned to us?

编辑:每个人都不断地问为什么我认为这是更优雅。

everyone keep asking why do i think it's more elegant..

好,它就是这样,而不是IComperable,对象将有CompereTo方法,在默认情况下抛出异常或返回0。

well, it's just like that instead of IComperable, object would had CompereTo method, that by default throwing exception or returning 0.

有不能,不应该被描述为字符串对象。对象可以有同样返回的String.Empty Type.FullName 只是一个任意的选择。

there are objects that cannot and should not be described as string. object could have equally return string.Empty. Type.FullName is just an arbitrary choose..

和方法,如Console.Write(对象),我认为它应该是:写(IStringable)

and for methods such as Console.Write(object), i think it should be: Write(IStringable).

不过,如果您使用的WriteLine任何东西,但字符串(或东西,它的ToString是显而易见的,如数字),在我看来,这是仅用于调试模式。

however, if you are using WriteLine to anything but strings (or something that its ToString is obvious such as numbers), it seems to me it's for debugging mode only..

顺便说一句 - 我应该怎么评价你呢?是不是好,我发布一个答案呢?

by the way - how should i comment to you all? is it okay that i post an answer?

推荐答案

有那恕我直言,应该不会被添加到System.Object的...

There are three virtual methods that IMHO should have never been added to System.Object...

  • 的ToString()
  • GetHash code()
  • 等于()

所有这些都按照你的建议与接口可能已付诸实施。如果他们这样做了,我认为我们会好得多。那么,为什么这些问题?让我们只专注于toString()方法:

All of these could have been implemented as you suggest with an interface. Had they done so I think we'd be much better off. So why are these a problem? Let's just focus on ToString():

  1. 如果toString()方法有望被别人使用的ToString(),并显示你有编译器不能强制执行隐性契约的结果来实现。你假设的ToString()被重载,但是没有办法迫使那个是这种情况。
  2. 随着IStringable你只需要添加到您的泛型类型约束或派生的接口从它需要它的使用上实施的对象。
  3. 如果您发现重载的ToString()的好处是调试器,你应该开始使用[System.Diagnostics.DebuggerDisplayAttribute]。
  4. 至于需要此实现为经由的String.Format()将对象转换为字符串,和/或Console.WriteLine,他们可以推迟到System.Convert.ToString(对象),并检查是否像'IStringable',故障切换到该类型的名称,如果没有实现。
  5. 在克里斯托弗Estep指出,这是特定的文化。

所以我猜我一人在这里说,我讨厌System.Object的和它的所有虚拟方法。但我爱C#作为一个整体,总体来说,我觉得设计师的表现非常出色。

So I guess I stand alone here saying I hate System.Object and all of it's virtual methods. But I do love C# as a whole and overall I think the designers did a great job.

请注意:如果你打算依靠的ToString()的行为超负荷,我建议你继续和定义IStringable接口。不幸的是,你必须选择另一个名称的方法,如果你真的想需要它。

Note: If you intend to depend upon the behavior of ToString() being overloaded, I would suggest you go ahead and define your IStringable interface. Unfortunatly you'll have to pick another name for the method if you really want to require it.

更多

我的同事和我刚才在谈的话题。我认为另外一个大问题的ToString()是回答它是干什么用的?。难道是显示文字?序列化的文字?调试文本?完整类型名称?

My coworkers and I were just speaking on the topic. I think another big problem with ToString() is answering the question "what is it used for?". Is it Display text? Serialization text? Debugging text? Full type name?

这篇关于为什么object.ToString()存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆