60-Second Version: Class = design of a car. Object = real car you drive. Use new to build it.
1. The Design Sheet = Class
A class defines what data and actions every car will have. No real car exists yet.
public class Car
{
public string Owner; // Data: Every car has an owner
private int engineNumber; // Data: Hidden from outside
public void Start() // Action: Every car can start
{
Console.WriteLine($"{Owner}'s car started");
}
}
2. Build the Real Car = Object
new takes the design and builds 1 real car in memory.
Car kunalCar = new Car();
kunalCar.Owner = "Kunal"; // OK: Owner is public
// kunalCar.engineNumber = 1; // Error: engineNumber is private
kunalCar.Start();
3. Access Control: public vs private
public = Anyone can use it. private = Only code inside the class can use it. Default is private for fields.
Beginner Trap: Car c; c.Start(); crashes with NullReferenceException. You drew the design but never built the car with new.
1. Why Do We Need Classes?
Without classes, managing 3 cars looks like this:
string kunalCarOwner = "Kunal"; int kunalCarSpeed = 0;
string prernaCarOwner = "Prerna"; int prernaCarSpeed = 0;
Classes group related data into one unit:
public class Car
{
public string Owner;
public int Speed;
}
2. Fields & Methods
Fields = Data the object stores. Methods = Actions the object can do.
public class Car
{
public string Owner; // Field
private int Speed; // Field - private
public void Accelerate() // Method
{
Speed += 10; // OK: Can access private Speed inside class
}
}
3. Constructors: The Factory Setup
A constructor runs automatically when you use new. Used to set initial values.
public class Car
{
public string Owner;
public Car(string ownerName)
{
Owner = ownerName;
Console.WriteLine($"New car for {Owner} rolled out");
}
}
Car sanjuCar = new Car("Sanju");
If you do not write one, C# gives a free empty constructor: public Car(){}
4. All Access Modifiers: Who Gets the Keys?
Access modifiers control visibility. Here are all 6:
| Modifier | Same Class | Same Project | Child Class | Outside Project |
public | โ
| โ
| โ
| โ
|
private | โ
| โ | โ | โ |
protected | โ
| โ | โ
| โ |
internal | โ
| โ
| โ | โ |
protected internal | โ
| โ
| โ
| โ |
private protected | โ
| โ | โ
if same project | โ |
Most used:
public class Car
{
public string Owner; // Everyone can see
private int engineNumber; // Only Car code can see. Default for fields.
protected string VinNumber; // Car + child classes like SportsCar can see
internal string FactoryCode; // Any code in same project can see
}
Rule of thumb: Start with private. Make it public only if other code needs it. Use protected when you plan for inheritance.
5. static: Shared by All Cars
Instance fields: Each car has its own Owner.
static fields: One value shared by all cars.
public class Car
{
public string Owner; // Instance
public static int TotalCars; // Static
public Car() { TotalCars++; }
}
Car a = new Car();
Car b = new Car();
Console.WriteLine(Car.TotalCars); // 2. Use ClassName for static.
6. How Memory Works: Parking Lot Analogy
Car kunalCar = new Car();
- Heap = Parking Lot: The real Car object is built and parked here.
- Stack = Valet Ticket: The variable
kunalCar is just a ticket holding the parking spot number.
Car anotherCar = kunalCar; copies the ticket. Both point to same car. This is why objects are "reference types".
No comments yet. Be the first to share your thoughts!