Friday, March 09, 2007 5:00 AM
dodyg
C# 3.0 Automatic Properties is a hack
ScottGu just made a post about the "automatic properties" on how it cut down on the clutter of defining the private field behind the property.
This feature is a hack and should be ignored.
The problem here are twofolds:
- You can only have one type of automatic properties, a read-write property.
- You have no access to the automatically generated private member
1: public class Nothing
2: { 3: public string Changes { get; set; } 4:
5: void Method()
6: { 7: _Changes = "nothing"; //WRONG
8: }
9: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Reason number 2 makes it impossible in using it a mix property environment where you have both read-write and readonly properties.
1: public class Nothing
2: { 3: public string Changes { get; set; } 4:
5: string _name;
6: public string Name { get { return _name; } } 7:
8: void Method()
9: { 10: this.Changes = "nothing";
11: this._name = "Hello"; //ARGGHH
12: }
13: }
Look how inconsistent and ugly the properties assignment are in the Method() method.
I don't know why they add this limited 'automatic properties' to the language where we are used to 'prop' and 'propg' anyway.