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