Bhubaneswar, Odisha
+91-8328865778

Class And Object IN C#

Class And Object IN C#

Class:

Class is user-defined datatype. Class is a collection of objects of same type that have properties and behaviour. C# is based on the C++ programming language. Hence, the C# programming language has in-built support for classes and objects. A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity.

  • The properties are used to describe the data the class will be holding.
  • The methods tell what are the operations that can be performed on the data.
  • For e.g Student is a class that having,
  • Properties: RollNo, Name, Std, Div etc.
  • Methods: Read(), Write(), etc.

Class Declaration:

// declaring public class
public class Demo
{

    // field variable
    public int a, b;

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

Object:

Object is a basic run-time entity Or we can say any real world thing that has state,identity and behaviour.

It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical C# program creates many objects, which as you know, interact by invoking methods. An object consists of : 

  • State: It is represented by attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It 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:

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.

Initializing an object

The new operator 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.

Example Of Class And Object:

// C# program to illustrate the
// 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
	public Dog(String name, String breed,
				int age, String color)
	{
		this.name = name;
		this.breed = breed;
		this.age = age;
		this.color = color;
	}

	// Property 1
	public String GetName()
	{
		return name;
	}

	// Property 2
	public String GetBreed()
	{
		return breed;
	}

	// Property 3
	public int GetAge()
	{
		return age;
	}

	// Property 4
	public String GetColor()
	{
		return color;
	}

	// Method 1
	public String ToString()
	{
		return ("Hi my name is " + this.GetName()
		+ ".\nMy breed, age and color are " + this.GetBreed()
		+ ", " + this.GetAge() + ", " + this.GetColor());
	}

// Main Method
public static void Main(String[] args)
	{		
		// Creating object
		Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
		Console.WriteLine(tuffy.ToString());
	}
}