Thursday 14 February 2013

When ever an object of class is created the constructor of that class gets called and the required memory get allocated. While creating the object of a class we are explicitly calling the constructor as following

  ConDemo cd = new ConDemo();  (Calling the constructor)

Constructors are of two types
  • Parameter-less  (zero argument) Constructor.
  •  Parametrized Constructor.

  •   Parameter-less  (zero argument) Constructor:   A constructor without any parameter is a parameter-less constructor which can be either defined implicitly provided there is no explicit constructor or else can be defined explicitly.
  •  Parametrized Constructor: A constructor with parameters is a Parametrized Constructor and can be defined only explicitly. if the constructor of a class is  Parametrized values to the parameter are sent while calling the constructor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class ConParam
    {
        int a;   // a is a class variable
        public ConParam(int a)   // a is a block variable
        {
            this.a = a;
        }
        public void display()
        {
            Console.WriteLine("Values of a is{0}", a);
        }
        static void Main()
        {
            ConParam p1 = new ConParam(10);
            ConParam p2 = new ConParam(20);
            ConParam p3 = new ConParam(30);
            p1.display();
            p2.display();
            p3.display(); 
            Console.ReadLine();
        }
     }
}
Constructors:

      It is a special method present under a class which is responsible for initializing the variables of class. The name of constructor method will b same as the class name and more ever it is a non value returning method.

Syntax to define a constructor:
[<modifiers>] <name> ([<parameter defination>])
   {
        Statements;
   }

      Every class requires a constructor to be present in it, if we want to create an object of that class.

Note: As the constructor is mandatory for creating the object of a class, it must be defined by the programmer explicitly, or else while compiling the program compiler takes the responsibility of defining a constructor implicitly under that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class ConDemo
    {
        public ConDemo()
        {
            Console.WriteLine("Constructor is called");
        }
        public void demo()
        {
            Console.WriteLine("Method is called");
        }
        static void Main()
        {
            ConDemo cd1 = new ConDemo();
            ConDemo cd2 = new ConDemo();
            ConDemo cd3 = cd2;
            Console.ReadLine();
        }
    }
}

Working with Objects in CSharp

If required the object of a class can be assigned to the variable of the same class and initialized it, so that the variable becomes reference of the class but references of a class will not have separate memory allocation. They will be provided with access to the memory of object and reference will be consuming the same memory. To test this rewrite the code under Main method of class first as following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First f1 = new First(); // f1 is a object
            First f2;              // f2 is a reference
            f2 = f1;
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x=300;
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x = 500;
            Console.WriteLine(f1.x + "   " + f2.x);
            Console.ReadLine();
        }
     }
}
/* In the above case, as the object and reference were consuming the same memory
 * any modifications that are performed on the members through 1 reflects on the
 * other also*/
We can create any number of objects to a class and each object we create will have separate memory allocation and any object doesn't reflect to the member's of other object. To test this rewrite the code under the Main method of class first as following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First f1 = new First();
            First f2=new First();
            Console.WriteLine(f1.x + "   "+ f2.x);
            f1.x=500;
            Console.WriteLine(f1.x + "   "+ f2.x);
            Console.ReadLine();
        }
     }
}
The object which is created can also be destroyed by assigning null to it. Once we assigned null to an object the memory which is allocated for object is marked as unused and cannot be accessed anymore. To test this rewrite the code under Main method of First class as following:

Note: The memory which is marked as unread will be deallocated by the Garbage Collector whenever it comes into a picture for deallocation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First obj = new First();
            Console.WriteLine(obj.x);  // Prints the value here
            obj = null;
            Console.WriteLine(obj.x);  // Error occurs here
            Console.ReadLine();
        }
     }
}
Every member of a class(variable or method) it it is non--static can be accessed under the Main method only by using object of class. A variable of a class is a copy of the class which is not initialized.
                        obj ----> null

An object of a class is a copy of a class which is initialized using new keyword and the memory is allocated for an object of class.
                        obj --->100=x

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First obj = new First();
            Console.WriteLine(obj.x);    //obj is a object or instance 
            
            Console.ReadLine();
        }
     }
}
// It will print the value of x=100