C# · 12月 30, 2021

c# – 由于Int32是一个值类型,为什么它继承.ToString()?

These are the docs关于.ToString()提示了这个问题.他们说:

Because Object is the base class of all reference types in the .NET
Framework,this behavior [.ToString()] is inherited by reference types that do not
override the ToString method.


For example,the base types such as Char,Int32,and String provide ToString implementations


那么这里发生了什么? Int32实现它是非常自己的.ToString()与Object无关?

解决方法 Int32是一个结构体,因此是一个值类型.
但: System.Object System.ValueType system.int32

Int32派生自System.ValueType,它本身来源于System.Object. Etvoilà…