Classes and Objects in C#

From the C# Programming Language cheat sheet · Object-Oriented Programming · verified Jul 2026

Classes and Objects

Defining classes and creating objects

csharp
// Simple class
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void Introduce()
    {
        Console.WriteLine($"Hi, I'm {Name}");
    }
}

// Usage
Person person = new Person();
person.Name = "Alice";
person.Age = 30;
person.Introduce();
💡 Auto-properties provide shorthand for simple get/set operations
⚡ Constructor initializes object state when created
📌 Access modifiers control visibility (public, private, protected)
🟢 Object initializer syntax allows setting properties during creation

More C# tasks

Back to the full C# Programming Language cheat sheet