473,220 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,220 software developers and data experts.

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 11805
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Author | last post by:
I wrote a simple Person class with Visual C# 2008 Express Edition, in which I define two properties: FirstName and LastName. If I use the new c# automatic property feature, it *compiles with no...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.