Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use base constructor data to another constructor in the same class?

while trying to call base constructor it throw error like "object doesn't contain constructor that take one argument"

    public string FirstName { get;private set; }
    public string LastName { get;private set; }

    public Employee(string firstName)
    {
        FirstName = firstName;
    }

    public Employee(string firstName,string lastName):base(firstName)//error
    {
        LastName = lastName;
    }

    public string SayHello()
    {
        return FirstName + " " + LastName;
    }

thanks

like image 588
Nikhil V Avatar asked May 06 '26 18:05

Nikhil V


1 Answers

You'd probably want to call current class constructor, not a base class constructor:

public Employee(string firstName, string lastName): this(firstName) // this, not base
{
    LastName = lastName;
}
like image 196
Dmitry Bychenko Avatar answered May 09 '26 07:05

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!