Open In App

C# Class and Objects

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single unit. In C#, classes support polymorphism, and inheritance and also provide the concept of derived classes and base classes.

Declaration of Class in C#

Generally, 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: Used to define accessibility levels by default modifier of the class is internal.
  • Keyword class: A class keyword is used to declare a class.
  • Class Identifier: The variable of type class is provided. The identifier(name of the class) and the initial letter should be capitalized by convention.
  • Base class(Optional): If it inherits its parent class(superclass), if any, then preceded by the : (colon).
  • Interfaces(Optional): A comma-separated list of interfaces implemented by the class, if any, preceded by the : (colon). A class can implement more than one interface.
  • Body: The class body is enclosed by { } (curly braces) and it may contain the class’s members like properties, methods, constructors events etc.

Example:

C#
// Declaration of class
using System;

// declaring public class
public class Geeks
{
	// field variable
	public int a, b;

	// member function or method
	public static void display()
	{
		Console.WriteLine("Class & Objects in C#");
	}

	public static void Main(String[] args)
	{
		display();
	}
}

Output
Class & Objects in C#

Objects in C#

Object is a basic unit of Object-Oriented Programming and represents real-life entities. It can also refer to the instance of a class. An object of that class is created. An object holds data and can access methods and their properties. 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.

Consider a Dog as an object and see the below diagram for its identity, state, and behaviour.

Class-and-Object


Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.

Declaring Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

Example:

Class-and-Object


As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, the type must be strictly a concrete class name. 

Dog tuffy;

If we declare a reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.

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.

C#
// Initialization of an object
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

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.

Class-and-Object

Important Points

  • Class and Objects are the fundamental concepts of OOPs and using these we can achieve different aspects like Inheritance, encapsulation abstraction etc.
  • For initializing the values we can use the constructor which makes the process more compact as shown in the above example
  • When an object is created, memory is allocated to store its data and the object’s address is assigned to the variable.
  • And for memory utilization, we can use Destructor to destroy the instance. If its work is completed no further is needed.


Next Article
Article Tags :

Similar Reads