#34 – The object Type

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();

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #34 – The object Type

  1. Pingback: #659 – Get Information About an Object’s Type « 2,000 Things You Should Know About C#

  2. Pingback: #771 – Summary of System.Object Members « 2,000 Things You Should Know About C#

  3. Pingback: #950 – C# Has a Unified Type System | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: