Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In C#, object is a language keyword that serves as an alias for the .NET System.Object class. It is the ultimate base class of the Common Type System (CTS). Almost every type in C#—including value types, reference types, predefined types, and user-defined types—inherits directly or indirectly from System.Object. Because it sits at the root of the type hierarchy, a variable of type object can hold a reference to an instance of most types. However, there are strict exceptions in modern C#: ref struct types (such as Span<T>) and unmanaged pointer types (such as int*) cannot be boxed or assigned to an object variable.

Memory and Type Mechanics

object is inherently a reference type. When an object instance is allocated on the managed heap, the Common Language Runtime (CLR) attaches an object header. This header contains a MethodTable pointer (which identifies the exact type) and a SyncBlock index. The SyncBlock index allows any object instance to serve as a monitor for thread synchronization. This mechanical detail is the foundational reason plain object instances are instantiated for use with the C# lock keyword. When interacting with value types, the CLR performs specific memory operations to bridge the gap between value semantics and reference semantics:
  • Boxing: The implicit conversion of a value type to an object. The CLR allocates a new object instance on the managed heap (including the standard object header), copies the value into that instance, and returns a reference to it.
  • Unboxing: The explicit conversion of an object back to a value type. The CLR verifies the exact type at runtime and copies the value from the heap to the target variable’s memory location.
// Heap allocation of a plain object, utilizing its SyncBlock index for synchronization
object syncRoot = new object();
lock (syncRoot) 
{ 
    // Critical section
}

// Boxing: Value type (int) implicitly converted to object (heap allocation)
int localValue = 42;
object boxedValue = localValue; 

// Unboxing: Object explicitly cast back to value type
int unboxedValue = (int)boxedValue; 

Type Safety and Resolution

Variables declared as object are subject to static typing at compile-time, meaning the compiler only recognizes the members defined on System.Object. To access members of the underlying type stored within the object, you must perform an explicit cast or use type-testing operators (is or as). Incorrect casting results in a runtime InvalidCastException.
object data = "Technical Documentation";

// Compile-time error: 'object' does not contain a definition for 'Length'
// int len = data.Length; 

// Type-checking and casting required to access underlying members
if (data is string stringData)
{
    int len = stringData.Length; 
}

Core API

Because almost all types inherit from System.Object, instances implicitly possess the following foundational methods, categorized by their access and inheritance modifiers: Public Static Methods:
  • Equals(object objA, object objB): Safely evaluates equality between two objects. It handles potential null references for either parameter before falling back to the instance-level virtual Equals method.
  • ReferenceEquals(object objA, object objB): Determines whether the specified object instances point to the exact same memory address.
Public Virtual Methods (Can be overridden by derived classes):
  • Equals(object obj): Determines whether the specified object is equal to the current object. The base System.Object.Equals method performs reference equality. System.ValueType overrides this method to perform value equality (a field-by-field comparison).
  • GetHashCode(): Serves as the default hash function, returning an integer used for insertion and retrieval in hash-based collections.
  • ToString(): Returns a string that represents the current object. By default, it returns the fully qualified type name.
Public Non-Virtual Methods (Cannot be overridden):
  • GetType(): Returns the System.Type instance representing the exact runtime type of the current instance.
Protected Methods (Accessible only within the class or derived classes):
  • MemberwiseClone(): A non-virtual method that creates a shallow copy of the current object.
  • Finalize(): A virtual method that allows an object to attempt to free unmanaged resources and perform other cleanup operations before it is reclaimed by garbage collection. C# explicitly forbids calling or overriding Finalize() directly. Developers must use destructor syntax (e.g., ~ClassName()), which the compiler translates into an override of this virtual method.
Master C# with Deep Grasping Methodology!Learn More