Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Default Constructor

Default Constructor:

A special method of the class without any parameter, with same name (Class name) that will be automatically invoked when an instance of the class is created is called as default constructor.

Or

Constructor without any parameter is called as default constructor.

Points to remember:

Using default constructor it is not possible to initialize each instance of the class to different values.
If other constructor is not exists in class, class injects default constructor.

Syntax:

[Access Modifier] ClassName([Parameters])
{
 
}


Example:

public class student
     {
        private static int numberOfStudents = 0;
        public student()
        {
            numberOfStudents++;
        }
 
        public static int NumberOfStudents
        {
 
            get
            {
                return numberOfStudents;
            }
            set
            {
                numberOfStudents = value;
            }
        }
    }
class Program
    { 
        static void Main(string[] args)
        {
            var SchoolStudent = new student();
            var GraduateStudent = new student();
            var PostGraduateStudent = new student();
            var StudentCount = student.NumberOfStudents;
            Console.WriteLine("Number of students: {0}", StudentCount.ToString());
 
          
        }
}
 


Output:


 

Parameterized Constructor

Parameterized Constructor:

There are five types of constructors in C#

Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private Constructor

In this article I will explain Parameterized Constructor with example.

Definition:

As name suggests constructor with at least one parameter is called as parameterized constructor.

Advantage using:

You can initialize each instance of the class to different values.
A Parameterized constructor can be defined explicitly only.

Syntax:

class SimpleClass
    {
        // Static constructor, with at least one parameter
        public SimpleClass(parameter 1,...paramter n)
        {
            //...
        }
    }

 

Example:

class Program
    {
        static void Main(string[] args)
        {
            Product MicromaxMobile = new Product("NotExpired");
            
            var isProductavailable = MicromaxMobile.isProductSellable();
        }
        }
 


public class Product
    {
        string ProductUseCategory;
 
        //parameterized constructor
        public Product(string cetgory)
        {
            ProductUseCategory =cetgory;
        }
       
        public int ProductID { get; set; }
 
        public string ProductName { get; set; }
      
        public double? UnitPrice { get; set; }
       
        public bool isProductSellable()
        {
            if (ProductUseCategory== "Expired")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
       
    }
 
 

 

 

Static Constructor

Static Constructor:

A static constructor is used to initialize static data only once, or used to perform action only once.

It will be executed only once, unlike the normal constructor that is executed whenever the object is created.

Syntax:

    class SimpleClass
    {
        // Static constructor
        static SimpleClass()
        {
            //...
        }
    }

Points to remember:

1.  Static Constructor cannot be called directly.
2.   No access modifiers allowed with Static Constructor.
3.   Parameters not allowed in Static Constructor.
4.   A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
5.  The user has no control on when the static constructor is executed in the program.
6.  The static constructor for a class executes before any instance of the class is created.
7.  Static constructor executes only one time.

Example:

In following example constructor EmployeeLeave checks weather current day is working day or week end day. If current day is Saturday or Sunday constructor sets static field value WorkingDay to false so Employee is not eligible to apply leave.  

public class EmployeeLeave
    {
        private static bool isWorkingDay;
 
        public string EmployeeID {get;set;}
        public decimal remaningLeaves{get;set;}
        public DateTime FromDate {get;set;}
        public DateTime TodateDate {get;set;}
 
        static EmployeeLeave()
        {
            //Getting the Current Date using DateTime Class 
            DateTime currentDate = DateTime.Now; 
 
            //Checking the Weekend as it is Saturday or Sunday 
            if(currentDate.DayOfWeek==DayOfWeek.Saturday|| currentDate.DayOfWeek==DayOfWeek.Sunday) 
            { 
                //If the Current Date is Saturday or Sunday 
                //consider as holiday  
                isWorkingDay =false
            } 
             
            //Days Other than Weekends(i.e other than Staurday or Sunday) 
            else 
            { 
                //If it is not weekend 
                //Consider working day
                isWorkingDay = true
            }            
        }
 
        public void LeaveApplication()
        {
            if(isWorkingDay==true)
            {
                //leave application Code
            }
            else
            {
                //Warning message ,tody is saturday or sunday
            }
        }
    }