> ## 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.

# C# object

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.

```csharp theme={"dark"}
// 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`.

```csharp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
