Sunday, May 9, 2021

Parameterized Constructor

In the class constructor contain at least one parameter is called parameterized constructor. The advantage of the parameterized constructor is that we can initialize each instance with different values at the time of object creation of the class.

Following is the example of the parameterized constructor:


using System;
                    
public class Program
{
    public static void Main()
    {
        Employee emp=new Employee(101,"Aman");
        Console.WriteLine("Id of Employee {0}",emp.Id);
        Console.WriteLine("Name of Employee {0}",emp.name);
    }
}
public class Employee{
    public int Id;
    public string name;
    public Employee(int id,string empName){
        this.Id=id;
        this.name=empName;
    }
}
 
Output : 
Id of Employee 101
Name of Employee Aman
 
 

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...