CSharp - System.Object Type

Introduction

object(System.Object) is the ultimate base class for all types.

Any type can be upcast to object.

The following code create a general-purpose stack.

class Stack
{
       int position;
       object[] data = new object[10];
       
       public void Push (object obj)   { 
          data[position++] = obj;  
       }
       
       public object Pop(){ 
          return data[--position]; 
       }
}

Stack works with the object type, and we can Push and Pop instances of any type to and from the Stack:

Stack stack = new Stack();
stack.Push ("book2s.com");
string s = (string) stack.Pop();   // Downcast, so explicit cast is needed
Console.WriteLine (s);             // book2s.com

object is a reference type.

We can auto box and auto unbox value types, such as int, to and from object.

This feature is called type unification:

stack.Push (3);
int three = (int) stack.Pop();

Object Member Listing

Here are all the members of object:

public class Object
{
       public Object();
       public extern Type GetType();
       public virtual bool Equals (object obj);
       public static bool Equals  (object objA, object objB);
       public static bool ReferenceEquals (object objA, object objB);

       public virtual int GetHashCode();

       public virtual string ToString();

       protected virtual void Finalize();
       protected extern object MemberwiseClone();
}