. Let's see about constructors with some examples in this chapter.
is a specialized method which is used to create a usable block that is executable from the objects created by
or structs.
Example:
Let's have an example code which explains about the constructors clearly,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Vehicle
{
string Name;
public Vehicle(string Yamaha) // Constructor of class Vehicle
{
Name = Yamaha;
}
}
class Program
{
static void Main()
{
Vehicle v1 = new Vehicle("R15"); // Calls the constructor
Vehicle v2 = new Vehicle("Fz"); // Calls the constructor
}
}
}
In the above code, we have a constructor for a
class
vehicle. In the
Main program, we are calling the constructor from the
object created by using
new
keyword.
Types of constructors
Based on different operations, constructors are categorized into many types. They are,
Default constructor
We have already seen above that a constructor that have no parameters is called as a default constructor. C# compiler provides a default constructor to each and every class that we create. This is not shown in our C# code.
This default constructor is automatically removed by the compiler when you add a new explicit constructor.
default constructors doesn't have any parameters as well as any internal logic.
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Example
{
public int x, y;
// Default parameter less constructor
}
class Program
{
static void Main()
{
Example ex = new Example();
ex.x = 5;
Console.WriteLine(ex.x);
ex.y = 0;
Console.WriteLine(ex.y);
}
}
}
In the above code, we don't have any explicit constructor. But still the object is referring somewhere. That is to the
default constructor created by
C# compiler.
Now, run this above code and see the result,
Instance Constructor
These are the most commonly preferable constructors used to create instance members.
Public Constructors:
These are the instance constructors which are exposed to public and accessed by any other functions in our program.
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class MyClass
{
public int x;
public MyClass(int i)
{
i = x;
}
}
class Program
{
static void Main()
{
MyClass m = new MyClass(6);
m.x = 4;
Console.WriteLine(m.x); // prints 4
}
}
}
The above code is an example of a simple instance constructor. Generally instance constructors are public which allows all methods to access it. This is not recommended in programming practice.
Output of the above code will be,
Private Constructor:
Private constructors are highly recommended instance constructors and mostly used for the classes that only has static members.
Private constructors doesn't allow any other classes to create instances of this
class. That means we cannot call it externally.
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class MyClass
{
public int x;
private MyClass(int i)
{
i = x;
}
}
class Program
{
static void Main()
{
MyClass m = new MyClass(6); // This will throw compile time error
}
}
}
Observe the above code in which we are trying to create an object and calling it through private constructor.
This will gives a compile time error.
Static Constructor
These are used to initialize the static data that needs to be performed only once. Static constructor is called automatically after the creation of the first instance.
Static constructors are also called as type initializers. We cannot guarantee the execution time of static constructor and hence it is a lazy instantiation.
Static constructions can slow down the process of accessing to a class by the members and very hard to test.
Syntax,
class MyClass
{
static int a; // static variable.
static MyClass(int i)
{
.......; // only static members need to be initialized.
......;
}
}
Conclusion
In this article, we tried to understand constructors and their types in
C#. Hope you understand.
Thanks for reading.
Krishna.