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
andperson2
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
andset
.
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
Modifier | Description |
---|---|
public | Accessible anywhere |
private | Accessible only inside the class |
protected | Accessible inside class & derived classes |
internal | Accessible within the same assembly |
protected internal | Accessible in derived classes or same assembly |
π 7. Summary Table
Concept | Description |
---|---|
Class | Blueprint defining fields & methods |
Object | Instance of a class |
Field | Variable inside a class |
Property | Encapsulated field with get/set |
Method | Function inside a class |
Constructor | Special method to initialize objects |
Access modifiers | Control visibility (public , private , etc.) |
β Tip:
-
Class = blueprint, Object = instance.
-
Use properties instead of public fields for safety.
-
Use constructors to initialize objects conveniently.