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.

A class in C# is a user-defined reference type that serves as a blueprint for instantiating objects. It encapsulates state (data) and behavior (operations) into a single logical unit, forming the foundational structure of object-oriented programming in the .NET ecosystem. Because classes are reference types, their instances are allocated on the managed heap, and variables hold a memory address pointing to the object rather than the object’s actual data. Memory reclamation is handled non-deterministically by the .NET Garbage Collector.

Syntax Anatomy

The declaration of a class utilizes the class keyword, optionally preceded by access and class modifiers, and optionally followed by a base class and implemented interfaces.
[access_modifier] [class_modifier] class ClassName : BaseClass, IInterface1, IInterface2
{
    // Member declarations
}

Class Members

A class is composed of members that define its state and behavior.
  • Constants: Immutable values evaluated at compile-time. They are implicitly static.
  • Fields: Variables declared at the class scope. They represent the raw state of the object.
  • Properties: Members that provide a flexible mechanism to read, write, or compute the value of a private field. They utilize get, set, or init accessors to enforce encapsulation.
  • Methods: Functions defined within the class that execute specific behaviors or algorithms.
  • Constructors: Special methods invoked automatically during object instantiation (via the new keyword) to initialize the object’s state.
  • Events: Members that enable a class or object to notify other classes or objects when something of interest occurs, typically backed by delegates.
  • Indexers: Members that allow an object to be indexed in the same manner as an array using this[] syntax.
  • Operators: Custom implementations of standard operators (e.g., +, ==) for the class, defined using the operator keyword.
  • Nested Types: Types (such as classes, structs, enums, or interfaces) declared entirely within the body of the class, typically used to encapsulate internal implementation details.
  • Finalizers (Destructors): A special method (~ClassName()) invoked by the Garbage Collector to perform cleanup of unmanaged resources before the object’s memory is reclaimed.
using System;

public abstract class BaseProcessor
{
    public abstract void Execute();
}

public class DataProcessor : BaseProcessor, IDisposable
{
    // Constant
    public const int DefaultTimeoutMs = 5000;

    // Field
    private readonly int _bufferSize;

    // Property with auto-implemented accessors
    public string ProcessorId { get; init; }

    // Constructor
    public DataProcessor(int bufferSize, string processorId)
    {
        _bufferSize = bufferSize;
        ProcessorId = processorId;
    }

    // Method overriding base abstract method
    public override void Execute()
    {
        // Implementation
    }

    // Indexer
    public int this[int index]
    {
        get { return index * _bufferSize; }
    }

    // User-defined Operator
    public static DataProcessor operator +(DataProcessor a, DataProcessor b)
    {
        return new DataProcessor(a._bufferSize + b._bufferSize, $"{a.ProcessorId}_{b.ProcessorId}");
    }

    // Nested Type
    public class ProcessorState
    {
        public bool IsActive { get; set; }
    }

    // IDisposable implementation
    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}

Technical Characteristics

Inheritance Rules C# enforces single class inheritance. A class can inherit from at most one direct base class. If no base class is explicitly declared, the class implicitly inherits from System.Object. While limited to a single base class, a class can implement an unlimited number of interfaces. Access Modifiers Classes and their members utilize access modifiers to define their visibility scope:
  • public: Unrestricted access.
  • internal: Access limited to the current assembly (default for top-level classes).
  • private: Access limited to the containing class (default for class members).
  • protected: Access limited to the containing class and derived classes.
  • protected internal: Access limited to the current assembly OR derived classes.
  • private protected: Access limited to the containing class OR derived classes within the same assembly.
Class Modifiers The behavior and extensibility of a class can be altered using specific modifiers:
  • abstract: Indicates the class is incomplete and cannot be instantiated. It must be inherited, and it may contain abstract methods that concrete (non-abstract) derived classes are forced to implement.
  • sealed: Prevents the class from being inherited. This allows the runtime to apply certain optimizations, particularly in virtual method resolution.
  • static: Indicates the class cannot be instantiated and can only contain static members. In the modern .NET ecosystem, static data is scoped and loaded into memory once per AssemblyLoadContext.
  • partial: A contextual keyword and type modifier that allows the definition of a class to be split across multiple physical .cs files. The compiler merges them into a single class during compilation.
Master C# with Deep Grasping Methodology!Learn More