469,283 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Automatic property and constructor

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.
Aug 3 '08 #1
3 11670
On Aug 3, 2:19 pm, Author <gnewsgr...@gmail.comwrote:
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;
}
Aug 3 '08 #2
Author <gn********@gmail.comwrote:

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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 3 '08 #3
On Aug 3, 11:23*pm, Author <gnewsgr...@gmail.comwrote:
On Aug 3, 2:19 pm, Author <gnewsgr...@gmail.comwrote:


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;

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
exactly. You got it correct.

- Cnu
Aug 4 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.