How come automatic property does not work?

A

Author

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
problem*, but I don't get the value through the property.

If I use the regular old style property definition, then it works
perfect.

// does not work
public string FirstName
{
get;
set;
}

// does not work
public string LastName
{
get;
set;
}


// The following works.

public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
}
}

How come? Am I not using C# 3.0?
 
M

Marc Gravell

Can you define "don't get the value through the property.", ideally
with an example? That should be fine... are you sure you haven't left
the old fields in the class, and are looking at the old fields? (auto-
props define their own fields that you can't see via your code - you
should only talk to the properties).

Marc
 
A

Author

Can you define "don't get the value through the property.", ideally
with an example? That should be fine... are you sure you haven't left
the old fields in the class, and are looking at the old fields? (auto-
props define their own fields that you can't see via your code - you
should only talk to the properties).

Marc

Thank you for your reply. Pasted below is the cs file I am testing.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Person
{
private string firstName;
private string lastName;
public Person()
{
}
public Person(string fn, string ln)
{
this.firstName = fn;
this.lastName = ln;
}
public string FirstName
{
get;
set;

//get
//{
// return this.firstName;
//}
//set
//{
// this.firstName = value;
//}
}
public string LastName
{
get;
set;
//get
//{
// return this.lastName;
//}
//set
//{
// this.lastName = value;
//}

}
}

class P
{
public static void Main(string[] args)
{
Person p = new Person("John", "Doe");
Console.WriteLine(p.FirstName + " " + p.LastName);
Console.Read();
}
}
}
 
M

Marc Gravell

So exactly what I said, then ;-p

Throw away the two fields "firstName" and "lastName".
Update the constructor to talk to the properties (change the case).

this.FirstName = fn;
this.LastName = ln;

Don't stress thinking "but that is less efficient"; first it would be
completely unnoticeable, and second it is highly likely that the "JIT"
will "inline" these calls at runtime, making them pretty much
identical to talking directly to the fields.

Marc
 
A

Author

So exactly what I said, then ;-p

Throw away the two fields "firstName" and "lastName".
Update the constructor to talk to the properties (change the case).

this.FirstName = fn;
this.LastName = ln;

Don't stress thinking "but that is less efficient"; first it would be
completely unnoticeable, and second it is highly likely that the "JIT"
will "inline" these calls at runtime, making them pretty much
identical to talking directly to the fields.

Marc

Hokay, :) Thank you very much. I didn't know that we *must not* use
private fields for properties.
 
J

Jon Skeet [C# MVP]

Hokay, :) Thank you very much. I didn't know that we *must not* use
private fields for properties.

It's not a case that you "must not use private fields" for properties.
It's that automatically implemented properties introduce their own
(hidden) fields - they won't start implicitly using ones you've
declared.

Jon
 
M

Marc Gravell

Hokay, :) Thank you very much.  I didn't know that we *must not* use
private fields for properties.

Only for auto-implemented properties. Simply, that isn't what the
compiler is going to talk to, so you had (roughly speaking - the names
are very different) the code below.

Marc

class Person {
private string firstName, lastName;
private string compilerGenerated1, compilerGenerated2;

public string FirstName {
get {return compilerGenerated1;}
set {compilerGenerated1 = value;}
}
public string LastName {
get {return compilerGenerated2;}
set {compilerGenerated2 = value;}
}
public Person() {}
public Person(string fn, string ln) {
firstName = fn;
lastName = ln;
}
}
 
M

Martin Bonner

Hokay, :) Thank you very much. I didn't know that we *must not* use
private fields for properties.

That should be:
"must not use private fields for *automatic* properties".

If you want to directly access the private fields - write the
properties out in full. Personally, I usually find I want to do that
anyway, because I want to fire events when a property changes, or I
want to calculate the return value, or something.
 

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