Codectionary / Developer documentation / C#

Classes & Objects

A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.

Read the syntax

class defines a type; new calls a constructor to create an object. Fields and properties hold per-object state, while methods describe actions the object can perform.

A static member belongs to the type itself rather than to one instance. In a Program.cs that uses top-level statements, put executable statements before type declarations.

Syntax

public class ClassName {\n  // fields, properties, methods\n}

Examples

Basic Class Definition

Creating a simple class with fields, a constructor, and a method.

// Usage
Person person = new Person("Alice", 25);
person.Introduce();

public class Person
{
    public string Name;
    public int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public void Introduce()
    {
        Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
    }
}
  1. Creating a simple class with fields, a constructor, and a method.
  2. class defines a type; new calls a constructor to create an object. Fields and properties hold per-object state, while methods describe actions the object can perform.
  3. The constructor stores Alice and 25 in a Person object. Introduce reads those fields and prints Hi, I’m Alice and I’m 25 years old.

Creating Multiple Independent Objects

Each object created from a class has its own separate copy of instance data.

Person alice = new Person("Alice", 25);
Person bob = new Person("Bob", 30);

alice.Age = 26;  // only affects alice, not bob

Console.WriteLine($"{alice.Name}: {alice.Age}");  // Alice: 26
Console.WriteLine($"{bob.Name}: {bob.Age}");        // Bob: 30

public class Person
{
    public string Name;
    public int Age;
    public Person(string name, int age) { Name = name; Age = age; }
}
  1. Each object created from a class has its own separate copy of instance data.
  2. class defines a type; new calls a constructor to create an object. Fields and properties hold per-object state, while methods describe actions the object can perform.
  3. new creates two independent Person objects. Updating alice.Age to 26 does not change bob.Age, so the two printed ages are 26 and 30.

Static Members

static fields and methods belong to the class itself, shared across all instances rather than being per-object.

Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();

Console.WriteLine($"Total instances: {Counter.GetCount()}");  // 3
Console.WriteLine($"c2 ID: {c2.InstanceId}");                   // 2

public class Counter
{
    private static int count = 0;  // shared across all instances
    public int InstanceId;

    public Counter()
    {
        count++;
        InstanceId = count;
    }

    public static int GetCount() => count;
}
  1. static fields and methods belong to the class itself, shared across all instances rather than being per-object.
  2. class defines a type; new calls a constructor to create an object. Fields and properties hold per-object state, while methods describe actions the object can perform.
  3. Each constructor increments the shared static count, then copies it into an instance ID. Three constructions produce a count of 3 and c2 keeps ID 2.

Object Initializers

A concise syntax for setting properties immediately when creating an object.

Product laptop = new Product
{
    Name = "Laptop",
    Price = 999.99
};

Console.WriteLine($"{laptop.Name}: ${laptop.Price}");

public class Product
{
    public string Name { get; set; } = "";
    public double Price { get; set; }
}
  1. A concise syntax for setting properties immediately when creating an object.
  2. class defines a type; new calls a constructor to create an object. Fields and properties hold per-object state, while methods describe actions the object can perform.
  3. The initializer sets both public properties while constructing laptop. The final interpolation prints the chosen name and price; currency formatting is not automatic.

Best practices

  • Use meaningful, PascalCase class names that describe what the class represents (Customer, not customer or CustomerClass)
  • Prefer properties (get/set) over public fields for exposing data - it gives you a way to add validation later without breaking callers
  • Write one class per file, with the filename matching the public class name, matching standard C# project conventions
  • Follow the Single Responsibility Principle - each class should have one clear, well-defined purpose

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading