PDA

Click to See Complete Forum and Search --> : [3.0/LINQ] Property Declartation


Bombdrop
Dec 4th, 2007, 08:52 AM
Not really a question just wanting to see how others feel about the new property declaration

public string Name
{
get;
set;
}

as opposed to


private string name;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}

I personally prefer the older syntax as within a class the idea of calling the property never seems right to me I always call the private field just wanting to hear other coders opinions :)

VBGuy
Dec 4th, 2007, 12:35 PM
Our shop standard is unless you can get in the conference room in front of the the entire team and present an overwhelming case why you should use the private var we always call the property. You can probably guess we like the new syntax...

I'd much rather type

public string Name {get; set;}

fewer keystrokes, fewer opportunities to make a mistake.

This assumes of course that you have no need to do any coding in the getter or setter.

jmcilhinney
Dec 4th, 2007, 05:27 PM
The problem with accessing the private variable within the class is that it bypasses any logic you add to the property getter or setter. If it is specifically desirable to by pass that logic then you should access the variable but otherwise you should always access the property. One of the reasons for properties is to allow you to change the implementation without changing the interface. If you were to change the property implementation at some point then every place you access the variable within the class would bypass the new logic.

Fromethius
Dec 4th, 2007, 06:49 PM
That's awesome! I didn't know there was such a new feature in C# 2008! How cool is that? Way cool! So much less typing and confusion now and none of that, "should i access the variable or the propery stuff" and the, "should i put the private variable right next to the property or the other variables?"

Also, none of that think up crazy names for the private variables so when your application is converted to vb .net it will still work stuff.

Hah, this is way cool. :D

*very pleased*



Edit: Where do you find out about things like these? Is there a universal changelog avaiable somewhere on microsoft?

jmcilhinney
Dec 4th, 2007, 07:50 PM
Also, none of that think up crazy names for the private variables so when your application is converted to vb .net it will still work stuff.That was never a problem anyway. You just use "PropertyName" and "_propertyName" in both.

ComputerJy
Dec 5th, 2007, 12:25 AM
That was never a problem anyway. You just use "PropertyName" and "_propertyName" in both.
Hate that standard :)

Fromethius
Dec 5th, 2007, 05:09 AM
The underscores cut me at night =(

jmcilhinney
Dec 5th, 2007, 05:01 PM
The use of an underscore on variable names that provide a property value is unobtrusive and clear. That is something to strive for.