Automatic property and constructor

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.
 
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.

I think I got it, simply use the public property instead of the
private fields in the constructor like so:

public Person (string fn, string ln, int age)
{
this.FirstName = fn;
this.LastName = ln;
this.Age = age;
}
 
J

Jon Skeet [C# MVP]

I think I got it, simply use the public property instead of the
private fields in the constructor like so:

public Person (string fn, string ln, int age)
{
this.FirstName = fn;
this.LastName = ln;
this.Age = age;
}

Exactly. If you don't want the property to be publicly settable, you
can make the setter private.

Unfortunately, you can't make it a genuinely "readonly" property such
that you can only set the values in the constructor (and at the same
time get the autogenerated code to use a readonly field behind the
scenes). That would be nice, but...
 
D

Duggi

I think I got it, simply use the public property instead of the
private fields in the constructor like so:

public Person (string fn, string ln, int age)
{
    this.FirstName = fn;
    this.LastName = ln;
    this.Age = age;



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

exactly. You got it correct.

- Cnu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top