CKTIS / C#
object — the common base type
The object keyword is an alias for System.Object. It can hold references to objects of different types and boxed values. Learn how equality, reference identity, and hash codes differ, and how to recover a more specific type safely.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
object value = 42;
object instance = new object();
object? optional = null;object is an alias for System.Object, the root base type in the C# type system. It is itself a reference type. An object variable can refer to objects of different types, but cannot accept ref struct values or pointers. With nullable annotations enabled, object? expresses that null is allowed; it does not create a different runtime type.
Converting a value such as an int to object causes boxing: creating an object containing a copy of the value. Copying an existing object reference does not create another box. Unboxing requires a compatible value type: for a boxed int, the cast (int)value works, but (long)value throws InvalidCastException. The pattern value is int number safely checks the type and exposes the value. Unlike dynamic, object retains compile-time checking of member availability.
Object.Equals(a, b) handles null and compares objects using the type's virtual Equals method. The default instance implementation of Object.Equals checks reference identity, but types can override it to compare values. Object.ReferenceEquals(a, b) checks only whether the references point to the same instance; it returns true for two null references. The == operator for operands whose static type is object compares references, not the contents of boxed values.
Object.GetHashCode returns a hash code used by hash-based collections. Objects equal according to Equals must have the same code; matching codes do not prove equality. When overriding Equals, also override GetHashCode consistently with this rule. A hash code is not a persistent identifier and should not be stored for later comparisons across runs. These mechanisms require no preview features in a .NET 10 project.
Examples
using System;
object first = 42;
object second = 42;
object alias = first;
Console.WriteLine(Object.Equals(first, second));
Console.WriteLine(Object.ReferenceEquals(first, second));
Console.WriteLine(Object.ReferenceEquals(first, alias));
Console.WriteLine(first.GetHashCode() == second.GetHashCode());
if (first is int number)
{
Console.WriteLine(number + 1);
}Result
True False True True 43
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.
Sources
- C# language reference
- C# Keywords and contextual keywords - C# reference
- Built-in reference types (C# reference)
- Object.Equals Method
- Object.GetHashCode Method
- Object.ReferenceEquals(Object, Object) Method
- Boxing and Unboxing (C# Programming Guide)
- Official reference — learn.microsoft.com: api / system.object