Friday, May 12, 2023

Parameterize constructor in C#

What is parameterize constructor in C#?


 In C#, a parameterized constructor is a constructor method that takes one or more parameters to initialize the object's data members or properties with specific values.


A parameterized constructor allows you to create objects with different initial values, depending on the values passed as arguments to the constructor. This is useful when you want to create objects with specific characteristics or properties.


Here's an example of a parameterized constructor:


class Employee 

{

    public string Name { get; set; }

    public int Age { get; set; }


    // Parameterized constructor

    public Employee (string name, int age)

    {

        Name = name;

        Age = age;

    }

}


In this example, the "Employee" class has a parameterized constructor that accepts two parameters: "name" and "age". The constructor initializes the "Name" and "Age" properties of the object with the values passed in as arguments.


You can create an instance of the "Employee" class using the parameterized constructor as follows:


Employee p = new Employee ("Dheeraj Kumar", 25);


In this example, the "Name" property of the "Person" object is set to "Dheeraj Kumar", and the "Age" property is set to 25, which are the values passed in as arguments to the constructor.


Parameterized constructors provide a way to initialize objects with specific values, and they are commonly used in C# programming to create objects with custom properties or characteristics.


No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...