Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

C# object type

Reference for the C# object type: the root of every type, its four virtual methods, boxing and unboxing, type tests with is and as, and pitfalls.

object is the C# keyword for System.Object, the base class of every type: classes, structs, enums, delegates, arrays and even the built-in value types like int. A variable of type object can hold a reference to anything, which makes it the universal but untyped container of the pre-generics era.

Storing a value type in an object boxes it: the runtime allocates a heap object, copies the value into it and gives you a reference. Getting it back with a cast unboxes it. Boxing costs an allocation and hides the real type from the compiler, so modern code prefers generics and pattern matching over object.

C#
object o = 42;          // boxing
int n = (int)o;         // unboxing

Try it

C# One variable, many types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Boxing, unboxing and the members of object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public class Object
{
    public Object();
    public virtual bool Equals(object? obj);
    public virtual int GetHashCode();
    public virtual string? ToString();
    public Type GetType();
    public static bool Equals(object? a, object? b);
    public static bool ReferenceEquals(object? a, object? b);
    protected object MemberwiseClone();
    protected virtual void Finalize();
}
Member Description
ToString() Text form. The default returns the full type name; override it for readable output.
Equals(object) Value or logical equality. The default is reference equality for classes and field-by-field comparison for structs.
GetHashCode() Hash for dictionaries and sets. Override whenever you override Equals.
GetType() The exact runtime Type. Not virtual: it cannot lie.
ReferenceEquals(a, b) True when both refer to the same object.
MemberwiseClone() Shallow copy, available inside the class.

Errors. Unboxing to the wrong exact type, or unboxing null into a non-nullable value type, throws InvalidCastException or NullReferenceException. Calling a member on a null reference throws NullReferenceException.

Usage notes

  • Unbox to the exact type. A boxed int can only be unboxed to int (or int?), not long; convert after unboxing, as in (long)(int)boxed.
  • Boxing is silent. Passing an int to a parameter of type object, using a non-generic collection like ArrayList, calling an interface method on a struct through the interface, or formatting with string.Format can all box. Generics (List<int>) avoid it.
  • == on object compares references. object a = 1, b = 1; a == b is false. Use Equals.
  • Override Equals and GetHashCode together, or hash tables will lose your objects. Records and structs with record struct generate both.
  • Prefer patterns to casts. if (o is string s) tests and casts in one step with no exception. as returns null on mismatch but only works with reference and nullable types.
  • dynamic is different. dynamic turns off compile-time checking; object keeps it, so you must cast.
  • Nullable annotation. With nullable reference types, an object cannot be null, whereas object? can.