How can I use the data of the underlying constructor for another constructor in the same class?

when trying to call the base constructor, it throws an error like "the object does not contain a constructor that takes 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;
    }

      

thank

+3


source to share


1 answer


You probably want to call the current class constructor, not the base class constructor:



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

      

+8


source







All Articles