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.
object o = 42; // boxing int n = (int)o; // unboxing
Try it
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.
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
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
intcan only be unboxed toint(orint?), notlong; convert after unboxing, as in(long)(int)boxed. - Boxing is silent. Passing an
intto a parameter of typeobject, using a non-generic collection likeArrayList, calling an interface method on a struct through the interface, or formatting withstring.Formatcan all box. Generics (List<int>) avoid it. ==onobjectcompares references.object a = 1, b = 1; a == bisfalse. UseEquals.- Override
EqualsandGetHashCodetogether, or hash tables will lose your objects. Records and structs withrecord structgenerate both. - Prefer patterns to casts.
if (o is string s)tests and casts in one step with no exception.asreturnsnullon mismatch but only works with reference and nullable types. dynamicis different.dynamicturns off compile-time checking;objectkeeps it, so you must cast.- Nullable annotation. With nullable reference types, an
objectcannot be null, whereasobject?can.