In Object-Oriented Programming, classes and objects are fundamental concepts used to represent real-world concepts and entities.
- A class is a blueprint used to create objects with similar properties and behaviors.
- An object is an instance of a class.
For example, Dog is a class, while a specific dog like Tommy is an object of that class.

Classes
A class is a user-defined data type that encapsulates data and behavior. It can contain fields, properties, methods, events, and constructors. A class itself does not occupy memory until its objects are created.
Syntax:
class ClassName{
// Fields
// Properties
// Methods
}

In the above image, blueprint of class Dog is shown:
- Identity: "Name of dog"
- State/Attributes: Breed , Age, Color
- Behaviors/Methods: e.g., Bark, Sleep, Eat
Declaration of Class
A class declaration begins with the class keyword followed by the class name. However, some optional attributes can be used with class declaration according to the application requirement. Class declarations can include these components, in order:
- Modifiers: Define the accessibility of a class. By default, a class is internal.
- Keyword class: Used to declare a class.
- Class Identifier: The name of the class, conventionally starting with a capital letter.
- Base Class (Optional): Specifies a parent class to inherit from, using the : symbol.
- Interfaces (Optional): A comma-separated list of interfaces implemented by the class, also preceded by : A class can implement multiple interfaces.
- Body: Enclosed within { }, containing members like fields, properties, methods, constructors and events.
Example:
using System;
// declaring public class
public class Geeks
{
// field variables
public int a, b;
// member function or method
public void Display(){
Console.WriteLine("Class in C#");
}
}
Objects
An object in C# is something you create from a class, which represents a real-world entity and lets you use the data and actions defined in that class.
In C# an object consists of:
- State: It is represented by attributes of an object and reflects the properties of an object.
- Behavior: It is represented by the methods of an object and also reflects how an object interacts with other objects.
- Identity: It represents the unique reference of an object, which distinguishes it from other objects.
Note: Classes define the blueprint of state, behavior and identity. Objects are the real instances that actually hold the state, show behavior and carry identity.
Declaration of Object
When an object is created, the class is instantiated. All objects share the class's behavior, but each has its own unique state. A class can have multiple instances. Example:

When we declare a reference variable in C# (like Dog tuffy or Dog freedo), no memory for the object is allocated at that time. The variable only holds a null reference until we explicitly create an object using the new keyword.
Initialization of Object
The new keyword instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

When we create an object of the Dog class and pass the parameters in the constructor. So it allocates memory for these different objects and each object stores its own reference in memory, and the reference variable points to that object, as shown in the image.
using System;
// Class Declaration
public class Dog{
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class same name as class
public Dog(String name, String breed, int age, String color){
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
public String GetName(){
return name;
}
public String GetBreed(){
return breed;
}
public int GetAge(){
return age;
}
public String GetColor(){
return color;
}
public override String ToString(){
return "my name is: " + name +
"\nmy breed is: " + breed +
"\nmy age is: " + age;
}
public static void Main(String[] args){
// Creating object
Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
Console.WriteLine(tuffy.ToString());
}
}
Output
my name is: tuffy my breed is: papillon my age is: 5
Explanation:
- Dog class defines attributes (name, breed, age, color) and methods.
- A constructor initializes these attributes when an object is created.
- ToString() is overridden to return a formatted string of object details.
- In Main, an object tuffy is created and its details are printed.