Jonathan Starr's Blog
Thoughts on C#, Ajax, WCF, LINQ et al.
Site Sections
Home
Contact
Syndication
Login
Posts
70
Comments
66
Trackbacks
0
<< Instructional Video #3 for Mobile Phone Project management
|
Home
|
Thought Experiment: Reforming Meetings >>
Automatic Properties in C# - A Critique
improve
my
=>
'code'
In the latest release of Orcas, one of the new features provided is "Automatic Properties" which allows developers to use a shorthand like the following:
public class
Person {
public string
FirstName {
get; set;
}
public string
LastName {
get; set;
}
public int
Age {
get; set;
}
}
and the compiler knows to interpret this as
public class
Person {
private string
_firstName
;
private string
_lastName
;
private int
_age
;
public string
FirstName {
get
{
return
_firstName
;
}
set
{
_firstName
= value;
}
}
public string
LastName {
get
{
return
_lastName
;
}
set
{
_lastName
= value;
}
}
public int
Age {
get
{
return
_age
;
}
set
{
_age
= value;
}
}
}
Prima facie, this looks like a good thing. The developer is typing less code, and still has hooks in property accessors to perform validation, or needed side effects from value changes.
But I have a few problems with this shorthand.
Criticism 1
Why is this shorthand needed when there is a snippet (prop) that provides full property accessors, with better hooks for inserting custom actions like validation?
Criticism 2
I think that the shorthand looks too much like the signature of a method normally found in an interface, not a class. The only difference appears to be an access modifier. So I find the shorthand syntax is confusing...
What do you think?
Jonathan Starr
posted on Tuesday, January 15, 2008 8:00 PM
Print
Comments
#
re: Automatic Properties in C# - A Critique
Marcus
1/16/2008 4:15 AM
As http://geekswithblogs.net/robp/archive/2008/01/14/another-reason-to-avoid-simple-properties.aspx point's out you can't debug this 'auto-props'. Which is really a pitty!
Personally I like the snippet way more.
#
re: Automatic Properties in C# - A Critique
Carl
1/16/2008 8:10 AM
Yup, another vote here for the snippet or you could use the refactor menu option:
private string m_AProperty;
Press Ctrl+R,E at the end of this line and you get:
private string m_AProperty;
public string AProperty
{
get { return m_AProperty; }
set { m_AProperty = value; }
}
A lot quicker than typing the stuff for the automatic properties and it's more future proof - like you said, when you want to add validation you're gonna need to type the whole lot out anyway.
Does anyone know of a valid reason for automatic properties even existing - I'm sure there must be one. MS aren't the best at implementing features that are universally requested, so I can't see them going to the trouble of implementing one that isn't.
#
re: Automatic Properties in C# - A Critique
Jay Glynn
1/16/2008 8:26 AM
automatic properties, type inference, etc etc were all included to make what LINQ can do much easier and in some cases even possible. hey were exposed because it was felt that they could be handy was of accomplishing certain tasks that otherwise would be difficult. It was never intended that you use automatic properties all the time, only when the situation called for it.
Post Comment
Title
*
Name
*
Email
Url
Comment
*
Remember Me?
Please add 5 and 7 and type the answer here:
Enter the code shown above:
Archives
June, 2008 (3)
May, 2008 (2)
April, 2008 (3)
March, 2008 (4)
February, 2008 (1)
January, 2008 (13)
December, 2007 (19)
November, 2007 (12)
October, 2007 (10)
September, 2007 (3)
Post Categories
C#
Philosophy
Critique
JavaScript
Links
Software Design
Personal
Religion
Music
Language
Microsoft Live
Politics
Geopolitics
Humor
Physics
Thought Experiments
Businesses using ASP.NET
Friends
Finance
Microsoft Events
Sharepoint
CRM
Caffeine
Africa
Charity
Sports
Statistics
Privacy
Management
Software Development
WCF
Instructional Video
1CCN.COM
Mobile Phone Applications
Windows Movie Maker
Management
Financial Engineering
ASP.NET
SQL Server
Image Galleries
Fishing Trip
About the Author
Blogs
Scott Guthrie
Timothy Ferriss
GeeksWithBlogs
Bill Evjen
Kevin Grossnicklaus
Meridium Visual Studio Macros
Theo Moore
Geometry
Flatland!
Good topology problem
Moebius Transformations
Music
Amish Paradise
Chiron Beta Prime Christmas
Code Monkey
Don't Download this Song
I Feel Fantastic!
re: Your Brains
The Future Soon
What's the matter with parents today
White & Nerdy
Programming
Good Script # Intro
jQuery
LOL Code Compiler
Meridium Visual Studio .NET Macros
MyGeneration
MyGeneration
Refactor Code
SkyDrive
Storage
Skydrive
Videos
Trunk Monkey
News
Jonathan Starr is a developer in Saint Louis, MO. He holds an MBA in Finance from Columbia Business School and earned his MCSD from Microsoft.
All statements in this blog are personal opinions and do not reflect the opinions of his employer.
Related Sites
Tag Cloud
Windows Live Writer
more tags...
Copyright © 2005 Jonathan Starr
This work is licensed under a
Creative Commons License