Open In App

Class and Objects in C#

Last Updated : 16 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, classes and objects are the core building blocks of object-oriented programming. A class provides a way to model real-world entities by grouping data and behavior together, while objects represent the actual entities created from these classes. This approach makes programs more organized, reusable and easier to maintain.

Classes

A class in C# is a user-defined type that encapsulates data and behavior. It can contain fields, properties, methods, events and constructors. A class itself does not occupy memory until objects are created from it.

Syntax:

class ClassName{

// Fields

// Properties

// Methods

}

class_in_c_

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 contains only a keyword class, followed by an identifier(name) of the class.

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:

C#
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

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.
  • Behaviour: It is represented by the methods of an object and also reflects the response of an object with other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.Declaring Objects (Also called instantiating a class)

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:

object_declaration_in_c_
Declaration of Object

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.

initialization_of_object_tuffy
Object initialization in C#

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 their address points with the class's object as shown in the image.

C#
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:

  1. Dog class defines attributes (name, breed, age, color) and methods.
  2. A constructor initializes these attributes when an object is created.
  3. ToString() is overridden to return a formatted string of object details.
  4. In Main, an object tuffy is created and its details are printed.

Article Tags :

Explore