Constructors in C#
Constructors are the special type of methods which are invoked automatically when the object of the class is created.Constructors are used to initialize the data members of the class.If no constructor is created then compiler will create default constructor .The default constructor initializes numeric fields 0 and string fields null.
- A Class can have any number of constructors
- Constructors don't have any return type.
- Within a class only one static constructor can be created.
- Static constructor can't be a parametrized constructor.
- only one static constructor can be created in a class.
- Default constructor
- Parametrized construtor
- Copy Constructor
- Static Constructor
- Private Constructor
This constructor will automatically invoked if we don't create any constructors. Default constructor doesn't take any parameters. It will initialize the same value to each instance of the class.
Example:1
Example:1
using System;
namespace saran
{
public class Program
{
public static void Main()
{
Default d = new Default();
Console.WriteLine(d.a);
Console.WriteLine(d.c);
}
}
class Default
{
public int a;
public string c;
}
}
output
0
Example:2
using System;
namespace saran
{
public class Program
{
public static void Main()
{
Default d = new Default();
Console.WriteLine(d.a);
Console.WriteLine(d.c);
}
}
class Default
{
public int a;
public string c;
public Default()
{
a=5;
c="ssn";
}
}
}
Output
5
ssn
ssn
2.Parametrized construtor
Constructor with parameter is called "Parametrized constructor". Different values can be initialized for each instance of the object through parametrized constructor.
Example:1
using System;
namespace saran
{
public class Program
{
public static void Main()
{
Test d = new Test(25,"ajith");
Console.WriteLine(d.age);
Console.WriteLine(d.name);
}
}
class Test
{
public int age;
public string name;
public Test(int c,string d)
{
age=c;
name=d;
}
}
}
Output
25
ajith
ajith
3.Copy Constructor
It copies the value from the existing object into new object.
Example
using System;
namespace saran
{
public class Program
{
public static void Main()
{
Test d = new Test(25,"ajith");
Console.WriteLine(d.age);
Console.WriteLine(d.name);
Test s =new Test(d);
Console.WriteLine(d.age);
Console.WriteLine(d.name);
}
}
class Test
{
public int age;
public string name;
public Test(Test t)
{
age =t.age;
name =t.name;
}
public Test(int c,string d)
{
age=c;
name=d;
}
}
}
output
25
ajith
25
ajith
ajith
25
ajith
4.Static Constructor
Static constructor is invoked only once for all instance of the class. It is invoked during the creation of first instance of the class.Static constructor initializes the static data members of the class.
- Static constructor doesn't have parameters and access modifiers
- Static constructor can't be called directly.
- Static constructor is called automatically when the first instance is created.
using System;
namespace saran
{
public class Program
{
public static void Main()
{
Test.show();
}
}
class Test
{
public static int age;
public static string name;
static Test()
{
if(age==0)
{
age=18;
}
if(name==null)
{
name="ajith";
}
}
public static void show()
{
Console.WriteLine("Static constructor works");
Console.WriteLine(name +"s age is "+age);
}
}
}
output
Static constructor works
ajiths age is 18
Comments
Post a Comment