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

Copy Constructor

Copy Constructor:

A special method of the class with same class instance 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

A parameterized constructor that contains a parameter of same class type is called as copy constructor.

Syntax:

[Access Modifier] ClassName([Class instance Parameter])
{
 
}


Example:

  public class student
    {
        public string StudentName {get;set;}
        public string TeacherName;
        public student(string teacher)
        {
            TeacherName = teacher;
        }
        //copy constructor
        public student(student Student)
        {
            TeacherName = Student.TeacherName;
        }
        public void DisplayStudentDetails()
        {
            Console.WriteLine("Student Name: {0}", StudentName);
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
    }
 
class Program
    {
        static void Main(string[] args)
        {
            var ITStudent = new student("Shivaji");
            ITStudent.StudentName = "Nagnath";
            var ElectronicsStudent = new student(ITStudent);
            ElectronicsStudent.StudentName = "Girish";
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
    }
    }


Output:


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;
            }
        }
       
    }