Class And Object In C#

A class is like a blueprint of a specific object. In the real world everything is an object of a particular type (class), every object has some attributes or features such as size, color, shape and some functionalities. For example Audi is an object of the car type. Car is a class that specifies certain characteristics like speed, color, shape, interior etc. So any company that makes a car that meets those requirements is an object of the car type. For example, every single car of Audi, BMW, and Jaguar are an object of the class called ‘Car'. Here, ‘Car' is a class and every single physical car is an object of the Car class.

In object oriented programming, a class is a template that defines the form of object. A class defines certain properties, fields, events, methods etc. A class defines the kinds of data and the functionality their objects will have. A class enables you to create your own custom types by grouping together variables of other types, methods and events.

 
The general form of a class is
  1. class <class name>  
  2.         {              
  3.               body of the class....    
  4.         }   
The body of class can consists of data members as well as methods.

The general form is expanded below to show the fact that the class body can contain data members (variables) as well as methods (function).

  1. access specifier class <class name>  
  2. {  
  3.      access specifier datatype variable 1;   
  4.      access specifier datatype variable 2;  
  5.      ............  
  6.      ............  
  7.      access specifier return type methodname1(parameter list)  
  8.         {  
  9.              ... body of method  
  10.          }   
  11.      
  12.     access specifier return type methodname2(parameter list)  
  13.         {  
  14.            ... body of method  
  15.         }  
  16.    }  
The access specifier is optional and if absent then the member is private to the class. Members with private access can be used only by the other members of the class. 
      
Let's understand it with an example. We are creating a class for box as below. 
  1. public class box  
  2. {  
  3.     double height;  
  4.     double width;  
  5.     double length;  
  6.   
  7.    public  void setval()   //method to assign values   
  8.     {  
  9.         height = 100;  
  10.         width = 180;  
  11.         length = 160;  
  12.     }  
  13.      
  14.   public void show()  
  15.    {  
  16.        Console.WriteLine("Height= " + height);  
  17.        Console.WriteLine("Width= " + width);  
  18.        Console.WriteLine("Length= " + length);  
  19.    }  
  20.      
  21. }  
 
Object is a runtime entity which is configured according to blueprint means class. Object is an instance of a class that is created dynamically. The box class is just a template for its object. Now we can create any number of objects of box class.
 
To create object of box class we have to use the following syntax,
 
<classname> <object name> =new <classname( )>
 
Lets have look at an example..
  1. public class boxdemo  
  2. {  
  3.     public static void Main(String []args)  
  4.     {  
  5.  font       box obj1 = new box();  
  6.           
  7.         // accessing box methods  
  8.         obj1.setval();  
  9.         obj1.show();  
  10.     }  
  11. }  
In boxdemo class we create an object of class box. The object obj1 can access methods of class box because of public access specifier. The data member of the class box can only access  the mehod ob box class, they can not access directly in boxdemo class because they are private by default.

The new operator dynamically allocates memory for the object. Each object has seperate memory allocation for its instance variables or data member is defined in the class.