A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-class-and-object/ below:

C# Class and Objects - GeeksforGeeks

C# Class and Objects

Last Updated : 11 Jul, 2025

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:

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 :

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

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:

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.

Important Points

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4