Let’s go through classes and objects in C# β€” the foundation of object-oriented programming (OOP).


πŸ“Œ 1. What is a Class?

A class is a blueprint or template for creating objects.

  • Defines data (fields/properties) and behavior (methods).

  • Think of it as a recipe, not the cake itself.

Syntax

[access_modifier] class ClassName
{
    // Fields
    // Properties
    // Methods
}
  • access_modifier β†’ public, private, etc.

  • Can contain fields, properties, methods, constructors, events, etc.


πŸ“Œ 2. What is an Object?

An object is an instance of a class β€” the actual β€œthing” created using the blueprint.

ClassName obj = new ClassName();
  • obj now has its own copy of the class’s fields and properties.

πŸ“Œ 3. Example: Basic Class and Object

// Class definition
public class Person
{
    // Fields
    public string Name;
    public int Age;
 
    // Method
    public void Greet()
    {
        Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
    }
}
 
// Create objects
Person person1 = new Person();
person1.Name = "Alice";
person1.Age = 25;
person1.Greet();  // Hello, my name is Alice and I am 25 years old.
 
Person person2 = new Person();
person2.Name = "Bob";
person2.Age = 30;
person2.Greet();  // Hello, my name is Bob and I am 30 years old.
  • Person is the class.

  • person1 and person2 are objects/instances.


πŸ“Œ 4. Constructors

A constructor is a special method that runs when an object is created. They have the same name as the class, which is the traditional way constructors are identified in C#. See 4.2 Constructors & Destructors

  • Usually used to initialize fields.
public class Person
{
    public string Name;
    public int Age;
 
    // Constructor
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
 
    public void Greet() => Console.WriteLine($"Hello, I am {Name}, {Age} years old.");
}
 
// Create object with constructor
Person person = new Person("Charlie", 28);
person.Greet(); // Hello, I am Charlie, 28 years old.
  • Can have multiple constructors (constructor overloading).

πŸ“Œ 5. Properties

  • Properties are a safer way to expose fields with get and set.
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
 
Person person = new Person();
person.Name = "Diana";
person.Age = 22;
Console.WriteLine($"{person.Name}, {person.Age}");
  • Properties can validate data before setting values:
private int age;
public int Age
{
    get { return age; }
    set 
    { 
        if (value >= 0) age = value; 
        else Console.WriteLine("Age cannot be negative"); 
    }
}

πŸ“Œ 6. Access Modifiers in Classes

ModifierDescription
publicAccessible anywhere
privateAccessible only inside the class
protectedAccessible inside class & derived classes
internalAccessible within the same assembly
protected internalAccessible in derived classes or same assembly

πŸ“Œ 7. Summary Table

ConceptDescription
ClassBlueprint defining fields & methods
ObjectInstance of a class
FieldVariable inside a class
PropertyEncapsulated field with get/set
MethodFunction inside a class
ConstructorSpecial method to initialize objects
Access modifiersControl visibility (public, private, etc.)

βœ… Tip:

  • Class = blueprint, Object = instance.

  • Use properties instead of public fields for safety.

  • Use constructors to initialize objects conveniently.