Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

C# 3.0 Syntax Additions—Design Guidelines : Page 3

These guidelines will help you understand new additions to C# 3.0 syntax and avoid some of the pitfalls you can encounter when using them.


advertisement
Object Initializers
Object initializers are something that should have been in C# from day one, to put it on par with many other high-level languages. In comparison to C/C++, this gives C# aggregate initialization abilities. This ability is probably the most generally useful of the capabilities added to support LINQ; Microsoft added object initializers because LINQ statements are single statements—there's no way to initialize an object with the results of a query unless it can be done within one statement.

Recommendations:
  • Prefer object initializers to one-property-per-statement object/element initialization.
Automatic Properties
Automatic properties are syntactic sugar for the tedious task of writing properties that wrap a single field for the backing store. In C# 2.0 you could implement a property like this:

private int age; public int Age { get { return age; } set { age = value; } }

That's not too much work; but when you need to implement many properties it can get tedious in a hurry. In C# 3.0 you can create properties without having to declare a field for the backing store:

public int Age { get ; set ; }

The compiler automatically generates the field used for the backing store, but you don't have access to it; all access to this property must go through the property getter/setter.

Recommendations:
  • Prefer automatic properties over public fields.
  • Consider avoiding automatic properties when performance is an issue.
Although Microsoft created much of the new C# 3.0 syntax for specific reasons, developers can use the new capabilities anywhere they're applicable. These guidelines offer advice that can help you avoid some of the pitfalls you may encounter when using the new C# 3.0 syntax, but they should not be considered as cast in stone. In rare circumstances you may have clear reasons to violate these guidelines, but typically, they should help you better understand the new C# 3.0 syntax and write better C# code.



Peter Ritchie has worked professionally in software development for over 17 years, and with computer software for far longer—his first computer was an Atari 800. Peter is president of Peter Ritchie Inc. Software Consulting Co., where he provides Windows-based software development services in Canada's national capital region.You can find his software-development-related pontification and advice on his blog. Peter received the Microsoft MVP-Visual C# award (2006-2007) for his contributions to the online developer community and is a contributor to the Visual C# Developer Center.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap