Wednesday, 16 January 2013

Constructors in C#.net

example of Constructors 
 
example of Default Constructor
 
public class x
{
       private int k;

    public x()

    {

        this.k = 1;

    }
 
 example of  Constructor overloading

public class x
{
    public int salary;

    public x(int annualSalary)
    {
        salary = annualSalary;
    }

    public x(int weeklySalary, int numberOfWeeks)
    {
        salary = weeklySalary * numberOfWeeks;
    }
}
how to call
 
x obj = new x(50000);
x obj1 = new x(90000, 48); 
 
how to call a Constructor  within a class
public x(int weeklySalary, int numberOfWeeks)
    : this(weeklySalary * numberOfWeeks)
{
} 
 
how to call a Constructor in derive class
public class y : x
{
    public y(int annualSalary): base(annualSalary)
    {
       
    }
}
 
-------example--------
public class x
{
    protected int i, j;
    
 public x(int a,int b)
 {
        i = 20;
        j = 30;
 }
    public int add()
    {
        return i + j;
    }
}
public class y:x
{
    public y(int b, int c) : base(b, c) 
    {
        int i = b;
        int j = c;
    }
    public int sub()
    {
        return i - j;
    }
}
here base class Constructor call first
 protected void Page_Load(object sender, EventArgs e)
    {
        y a = new y(80, 40);
        Response.Write(a.add().ToString()+"<br>");
        Response.Write(a.sub().ToString());
    }
out put 
50
-10

-------example--------
example of private constructor 
 
Private constructors, the constructors with the "private"
 access modifier, are a bit special case. It is because we can neither 
create the object of the class, nor can we inherit the class with only private constructors. But yes, we can have the set of public constructors along with the private constructors in the class and the public constructors can access the private constructors from within the class through constructor chaining. 

public class x
{
    private x() { }
    public static int i;
    public static int sqr()
    {
        return i*i;
    }
}
 protected void Page_Load(object sender, EventArgs e)
    {
      
x.i=2;
   Response.Write(x.add().ToString());
    }
out put 


public class Class1
{
    int x, y;
    private Class1()
    {
        x = 5;
    }
    public Class1(int a):this()
    {
        y = a;
    }
    public string add()
    {
        return x.ToString() + " " + y.ToString();
    }
}

 protected void Page_Load(object sender, EventArgs e)
    {
        Class1 o = new Class1(8);
        Response.Write(o.add().ToString());
    }

outpur
5 8
public class myClass
{
    private MyClass()
    {
        Console.WriteLine("This is no parameter Constructor");
    }

    public MyClass(int var):this()
    {
        Console.WriteLine("This is one parameter Constructor");
    }    
    // Other class methods goes here
}
Then we can create the object of this class by the statement:
MyClass obj = new MyClass(10);
The above statement will work fine, but the statement
MyClass obj = new MyClass();
will raise an error : 'Constructors.MyClass.MyClass()' is inaccessible due to its protection level

-------example--------
 static constructors
  1. There can be only one static constructor in the class.
  2. The static constructor should be without parameters.
  3. It can only access the static members of the class.
  4. There should be no access modifier in static constructor definition.  
  5. public class StaticTest
  6. {
      public static int SomeVarA;

      static StaticTest()
      {
        SomeVarA = 1;
      }
    }
     
public class x
{

public static int i ;
static x()
{

i = 5;

}

public int sqr()
{

return i * i;
}

}

 
x o=new x();
Response.Write(o.sqr().ToString());
out put
25 

No comments:

Post a Comment