#34 – The object Type
July 21, 2010 3 Comments
In the .NET Framework, all types are derived from System.Object. In C#, the object keyword is a synonym for this same type. System.Object is the base class for all other types in .NET, including built-in types and custom types.
In C#, all types can be upcast to object.
string msg = "A string"; int n = 42; Person me = new Person("Sean", 46); // Can assign anything to an object variable object o = msg; o = n; o = me;
Any class that you create in C# is automatically derived from object.
The object type defines the following instance methods:
- bool Equals(object)
- void Finalize()
- int GetHashCode()
- Type GetType()
- object MemberwiseClone()
- string ToString()
The object type defines the following static methods:
- bool Equals(object, object)
- bool ReferenceEquals(object, object)
This means that every new class automatically inherits these methods.
Person me = new Person("Sean", 46); int hash = me.GetHashCode();