A
Author
If we do automatic property, we don't declare the private fields
because the compiler will generate them for us.
Then, how do we implement a constructor that takes the private fields
as its parameters?
For example, the following Person class uses the automatic property
feature.
class Person
{
public string FirstName { get; set;}
public string LastName { get; set;}
public int Age { get; set;}
}
We can then instantiate a Person like this:
// I notice that the parentheses after Person below are optional.
Person person = new Person() { FirstName="John", LastName="Doe",
Age=21};
But, how can I define a constructor that takes the following form?
public Person(string firstName, string lastName, int age)
{
// I cannot proceed here because there are no
//explicit private fields if we do automatic properties.
}
Or is it the case that we then are not supposed to define such a
constructor?
Thank you if you could share your 2 cents.
because the compiler will generate them for us.
Then, how do we implement a constructor that takes the private fields
as its parameters?
For example, the following Person class uses the automatic property
feature.
class Person
{
public string FirstName { get; set;}
public string LastName { get; set;}
public int Age { get; set;}
}
We can then instantiate a Person like this:
// I notice that the parentheses after Person below are optional.
Person person = new Person() { FirstName="John", LastName="Doe",
Age=21};
But, how can I define a constructor that takes the following form?
public Person(string firstName, string lastName, int age)
{
// I cannot proceed here because there are no
//explicit private fields if we do automatic properties.
}
Or is it the case that we then are not supposed to define such a
constructor?
Thank you if you could share your 2 cents.