Search code examples
c#.nettostringvalue-typereference-type

Since Int32 is a value type why does it inherit .ToString()?


These are the docs about .ToString() that has prompted this question. They state:

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.

Yet further on it goes to state:

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

However Int32 is a struct and hence must be a value type.

So what's going on here? Does Int32 implement it's very own .ToString() which has nothing to do with Object?


Solution

  • Int32 is a struct and therefore a value type. But:

    System.Object
       System.ValueType
          System.Int32
    

    Int32 derives from System.ValueType and this itself derives from System.Object. Et voilà...