Wednesday 19 August 2015

What is Class , Object and Instance in .Net

According to OOP'S data must be represented in form of a class. A Class can contain variables for storing the data and methods to specify various operations that can be performed on data. A Class will not allocated memory for it's members. Hence it is only logical representation of data.
      A Class is only logical representation of data hence to work with data represented by the class. We must create a variable for that class , which is called as an Object. When we create an object for the class using the keyword then memory will be allocated for the members of that class with in the heap memory area.which is called as an instance and It's address will be stored within the object in stack.
      When we create an object for the class using the keyword 'new' then memory will not be allocated for the members of that class in heap memory area. i.e instance will not be created and object in the stack will contain NULL. In this case it will not be possible to access  the members of that class using that object .

Example :
        Using System;
Public class Class1
{
   int a =10;
 
  Public void Demo()
{
console.WriteLine("this is sample example");


      Public static void Main()
   {
      Class1 c = new Class1();

         c.Demo();

}
}
}

No comments:

Post a Comment