VS2010 "Public Property <PropertyName> As <DataType> vs. Public var
In VS2008, I used to input
Public Property <PropName> As <dataType>
and hit the Enter key and the IDE editor will automatically expand it to a fully functional property block.
Now from what I understand, the new 2010 feature is that the compiler will automatically "expand" the short syntax above into the same IL code you would get with the full set of GET and SET properties you were accustomed to seeing before in the editor.
But functionality like this heck is nothing more than just having a Public level variable? If the only diff is what it compiles, and if there is no functional difference, isn't the new way less efficient than the old one, since it contains more code than just a class-level memory variable?
Public <Variable> as <DataType>
I thought that if you didn't have any code behind your properties, they would be essentially the same. I think the difference is that they just added the "Property" keyword, but functionality, they don't have a diff, huh ??
In this particular case, this is not much different, but I never use Public data members - everything that needs to be exposed outside the class is always done with properties. This means there is a little more work to do when declaring them, but when you want to have property / accessor methods because you need to implement some code, it's much easier knowing that your property is already being used everywhere in the code. ...
Before someone pulls me in on this, no - it still isn't ... You can manipulate a public member using a link like ...
a source to share
This has to do with why properties are useful. They provide a layer of isolation between the implementation of the class and the client code that uses it. When you use a public field, you cannot easily refactor the way the field behaves, the client code refers to it directly. For example, changing a field to a property requires you to recompile all client code that uses it.
The usefulness of an automatic property is that it doesn't force you to decide ahead of time that refactoring might be needed. You can defer the decision and change it from an automatic property to an explicit custom behavior property at any time you like. Without making any changes to the client code.
The JIT compiler makes sure that an automatic property is as efficient as a field by inlining an accessor call. The new auto property syntax makes it just as effective on your wrists as it is in the public field. It's a complete win-win game, it just doesn't make sense to use the public field anymore.
a source to share
I'm not sure if I understand your question correctly.
But the need for a class-level public variable vs properties has already been discussed here.
EDIT: Also, the IDE / Compiler makes it easier for you to shorten the code if you just do get / set
eg.
public string Name { get; set; }
which doesn't require you to declare a background field.
But then you have to access that member (even within the class) using the property. Because the compiler creates a support field for you, but its name is unknown.
a source to share
The main difference is Auto-Implemented Properties ( VB ) and public Fields are interface definitions.
The codes that use your class with auto-implemented properties do not need to be changed if in the future you decide to add logic to the property, whereas if you use fields you will have to change the interface definition to a property.
Thus, auto-implemented properties use a simple public field syntax (no full property declaration), but with the flexibility of a property.
A little example :
Current code (C #):
class PersonA {
public int Age;
public int BirthYear;
}
class PersonB {
public int Age { get; set; }
public int BirthYear { get; set; }
}
Usage:
var john = new PersonA { Age = 30, BirthYear = 1980 };
var jane = new PersonB { Age = 20, BirthYear = 1990 };
If in the future you decide to drop the age setting and get a value from BirthYear, you can easily update your class without breaking any of the current client code.
class PersonA {
public int Age { get { return Date.Now.Year - BirthYear; }; set { } };
public int BirthYear;
}
class PersonB {
public int Age { get { return Date.Now.Year - BirthYear; }; set { } };
public int BirthYear { get; set; }
}
Usage:
var john = new PersonA { Age = 30, BirthYear = 1980 }; // broken when not recompiled
var jane = new PersonB { Age = 20, BirthYear = 1990 };
a source to share