What is an Object in OOP?
In object-oriented programming (OOP), an object is an instance of a class. A class is a blueprint, and an object is the actual usable form created from that blueprint. Objects contain data (attributes) and behaviors (methods).
Example in C#
Here is a simple class and object example:
class Car
{
public string Color;
public int Speed;
public void Accelerate()
{
Speed += 10;
}
}
Car myCar = new Car();
myCar.Color = "Red";
myCar.Speed = 60;
myCar.Accelerate();
Console.WriteLine(myCar.Speed); // Output: 70
Objects in Memory
In C#, objects are stored in the heap, while the reference variable (like myCar)
is stored on the stack. Each object has its own data and can call its own methods.

No comments:
Post a Comment