Auto-Implemented Properties c# .NET 3.0+

posted on Friday, December 26, 2008 1:00 PM

Instead of the following typical property setter/getter

private string courseName;
pubic string CourseName{
   get { return courseName;}
   set { courseName = value;}
}

 

//Auto-Implemented property implicitly creates an instance variable for property CourseName
public string CourseName {get; set;}

  • unable to use the private variable for this property (hidden from developer)
  • developer can only reference within the class via CourseName 

This is a reasonable approach for quick prototyping of properties only.

category: .NET Language Features

Comments


# re: Auto-Implemented Properties c# .NET 3.0+
Posted by Jeff Handley on 1/5/2009 8:29 PM
Gravatar I disagree about the "quick prototyping of properties only" statement. This can be very useful through and through. True that you don't have access to the private member, but most of the time that you would be accessing the private member, you could just be using the property.

You can also tweak the property by doing:

public string CourseName {get; private set; }

That makes the property readable publicly, but only settable privately. This is a common pattern for when you would otherwise be accessing the private member.

Something I've yearned for though is the ability to provide a pattern or template for properties, instead of the default template being applied.
# re: Auto-Implemented Properties c# .NET 3.0+
Posted by dave on 1/5/2009 8:37 PM
Gravatar Well, I can very easily see how an application can start out with the auto-implemented properties. Over time and as more complexity is established within the application some of the properties now need to evolve to full set/gets. Your interaction with the properties within the class can be inconsistent. Some will be referenced through this.FirstName while others will be like _lastName. It is my perspective that this type of code is difficult to follow and not optimum. It would seem appropriate that the code within the class gains value with consistency.
# re: Auto-Implemented Properties c# .NET 3.0+
Posted by Jeff Handley on 1/6/2009 1:25 AM
Gravatar Fair enough. For some smaller classes though, sometimes the complexity just isn't there. I follow the YAGNI principle on this and I leave them as auto properties until it feels wrong in the context.
Post Comment
Title *
Name *
Email
Url
Comment *
Please add 1 and 1 and type the answer here:

About Me

An engineer by training and a software developer at heart. My techniques and approaches meld engineering approaches with software technology.

Core to these principles is a systematic approach to the development of software with a strong lifecycle and process management emphasis through adoption of mature technologies.

Ten years designing heavy structural steel and concrete structures and 12 years in the software development profession have embedded strong project management and business knowledge in my approaches.

Subscribe to Rss Feed


Follow me on twitter @dyardy