November 23, 2014
Hot Topics:

.NET Tip: New Features in C# 3-Automatic Properties and Initializers

  • October 26, 2007
  • By Jay Miller
  • Send Email »
  • More Articles »

Automatic properties in C# 3 let you significantly cut down the amount of code needed to access class properties. With the new syntax, C# will create a private variable and default get/set methods using syntax that is very similar to public properties. If you need to do more than simply read from and write to a variable, you can still create properties as before, but many of your domain classes will be greatly simplified. The syntax for a simple Person class using automatic properties looks like this:

public class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
}

The addition of the get and set after the property name instructs the compiler to create a private field and default get/set accessors. Code that uses your Person class does not need to change. The other feature I would like to touch on is initializing objects. Object initializers let you combine the definition and initialization of objects in one concise line of code. Look at what your code might look like if you wanted to create and initialize an instance of the Person class above:

Person author = new Person();
author.Name   = "Jay Miller";
author.Age    = 40;

This doesn't look too bad, but imagine if the class had a dozen or more properties to initialize. One solution is to create multiple constructors or initialization functions that accept parameters and initialize the properties using those parameters. The challenge with this approach is that you either have to create methods that take every combination of properties you may need to initialize, or you create one method that can accept parameters for all properties. In the first case, you may end up with many constructors/initializers that clutter the interface. In the second case, you may need to pass many unnecessary parameters if you only want to initialize a single property. The new syntax solves these problems, allowing you to initialize only those properties needed without cluttering the interface. Here is how you could initialize an instance of the Person class:

Person author = new Person { Name = "Jay Miller", Age = 40 };

The new initializers are not limited to individual objects. If your class has nested complex types, such as an address, they can be initialized as well. Collections also can take advantage of initializers. You could initialize a list of authors using a collection like this:

List<Person> authors = new List<Person> {
   new Person { Name = "Jay Miller",  Age = 40 },
   new Person { Name = "Dr. Suess",   Age = 70 },
   new Person { Name = "Ed Emberley", Age = 50 }
};

This only touches the surface of how C# 3 and VS2008 can simplify your code. Take a look at the complete feature set of C# 3 to see how Microsoft is working to ease your job as a developer.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel