什么是背后的对象类型有类似的ToString(),GetHash code()的GetType等方法的原因 [英] What is the reason behind Object Type having methods like ToString(),GetHashCode(),GetType etc
问题描述
我有一个看起来很傻的问题。有没有像背后ToStrin(),GetHash code()的GetType()等方法确定的基本对象类型的任何具体原因。难道这些方法在内部使用由.NET Framework为任何目的。只是想知道,一直这样的设计背后的想法。
I have a question which might look silly. Is there any specific reason behind defining methods like ToStrin(),GetHashCode(),GetType() etc on the base Object Type . Are these methods internally be used by the .Net framework for any purpose. Just curious to know the thoughts that have been behind this design.
谢谢, sveerap
Thanks, sveerap
推荐答案
这些方法被认为是所有对象所需的最低限度。通过向他们介绍了 System.Object的
类,它们可在.NET中的所有对象。
These methods were considered the bare minimum required by all objects. By introducing them to the System.Object
class, they are available on all objects within .NET.
每个服务的目的:
-
等于
和的ReferenceEquals
用于相等检查
在散列集合中使用 -
GetHash code
允许对象 -
的GetType
需要反思工作 -
的ToString
允许所有对象,psented以字符串形式不论类型,需重新$ P $,这是非常有用 -
的Finalize
使资源得到妥善的垃圾收集处理 -
MemberwiseClone
允许对象的浅副本生成
Equals
andReferenceEquals
are used for equality checkingGetHashCode
allows objects to be used in hashed collectionsGetType
is required for reflection to workToString
allows all objects, regardless of type, to be represented in a string form, which is incredibly usefulFinalize
allows resources to be handled properly by the garbage collectorMemberwiseClone
allows for shallow copies of objects to be generated
这篇关于什么是背后的对象类型有类似的ToString(),GetHash code()的GetType等方法的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!