Hidden things about Constructor in C#
What's constructor?
It's a special method used to initialize an object or struct, limit access to object or control object or structs by set default values, In brief it's like the key to the door, let's say you want to run your car what do you need to do this? of sure you need the Key to run, so the Constructor in this case to run function of the Car has a Parameter called Key,
without the Key you cannot run
public class Car
{
string _key;
public Car(string key){ // the constructor, accepts string key as parameter
_key=key;
}
}
Parameterless constructors:
If you didn't create any constructors in your class, then C# creates
the default Parameterless Constructor that initialize
C# types default values, and the same on Structs, and if you
have Static Fields then C# by default creates
Static Constructor to initialize those Fields into default
value
Syntax:
The Constructor is always has the name of it's Type and it
doesn't have a return Type,
It can be Public or Private,
and the constructor can be Static
Private Constructor:
The Private constructor used to prevent creation of any
Instances outside your
Class and this is called Singleton Pattern or to control the
number of instances that can be created for your class
an example of usage for private Constructor is when you want to
create a sequence number like Invoice ID, see below code
public class Program
{
public static void Main()
{
Post cannotInitializePost=new Post(); // Compile error: Post is inaccessible due to its protection level
Post canInizializePost=Post.Instance; // this worked and initalize one instance of the class at the lifetime of the application
}
}
public class Post
{
private static Post instance;
private Post()
{
}
public static Post Instance
{
get{
if (instance == null)
{
instance = new Post();
}
return instance;
}
}
}
Static Constructor:
As we mentioned the Static Constructor used to initialize
Static Data, and also it can be used to initialize something for one
time only because
the Static Constructor is initialized before any Instance creation
or any Static Field reference, see below code
public class Program
{
static void Main()
{
new Home();
}
}
public class Home
{
public Home()
{
Console.WriteLine("Instance Constructor");
}
static Home(){
Console.WriteLine("Static Constructor");
}
}
// Results:
// Static Constructor
// Instance Constructor
Some Properties of the Static Constructors:
- Cannot have access modifiers
- Cannot have any parameters
- The class or struct can have only one Static Constructor
- The Static Constructor is initialized when a Static Method is invoked or assigned to Delegate or Event
Deconstructor:
The Deconstructor Pattern is initialized in C# 7.0,
It acts as an opposite to the Constructor, The Constructor takes
a set of values (parameters) and assign it to fields, while
Deconstructor assigns fields back to a set of variables
below is a sample code of Deconstructor:
public class Program
{
public static void Main()
{
var person = new Person("Amro Mustafa");
(string firstName, string lastName) = person;
Console.WriteLine(firstName + " " + lastName); // Amro Mustafa
}
}
class Person
{
public readonly string Name;
public Person(string name)
{
Name = name;
}
public void Deconstruct(out string firstName, out string lastName)
{
string[] nameArr = Name.Split(' ');
firstName = nameArr[0];
lastName = nameArr[1];
}
}
we used here a Tuples way to call the Deconstructor,
we can call it in many ways like below:
person.Deconstruct (out string firstName, out string lastName);
Comments
Post a Comment