In C#,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.
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
objectback 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.
Type Safety and Resolution
Variables declared asobject 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.
Core API
Because almost all types inherit fromSystem.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 potentialnullreferences for either parameter before falling back to the instance-level virtualEqualsmethod.ReferenceEquals(object objA, object objB): Determines whether the specified object instances point to the exact same memory address.
Equals(object obj): Determines whether the specified object is equal to the current object. The baseSystem.Object.Equalsmethod performs reference equality.System.ValueTypeoverrides 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.
GetType(): Returns theSystem.Typeinstance representing the exact runtime type of the current instance.
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 overridingFinalize()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





