September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

C# Tips and Tricks

  • February 9, 2009
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

As the title indicates, this article will be about C# tips and tricks. I'll cover some of the C# 3.0 language features, explore productivity gains with the Visual Studio 2008 C# IDE, and mention a couple of handy Visual Studio plug-ins that may be of interest. This deviates from the traditional articles I've presented in the past and will hopefully provide some value in its own way.

C# 3.0 Background

C# 3.0 was released as a part of the Microsoft .NET 3.5 Framework. The main purpose was to provide a foundation for Language INtegrated Query (LINQ) to provide unified data access capability. Each enhancement also can be used on its own. Here is a list of the features you'll cover and learn some related tips:

  • Automatically implemented properties
  • Using Keyword
  • Object and collection intitializers
  • Local type inference
  • Extension methods

Automatically Implemented Properties

Automatically implemented properties offer a concise syntax for implementing property accessors that get and set a private field. They create a field backed property without requiring you to actually create a field. The compiler automatically generates it for you at compile time. They can be used only for simple get and set of properties because there is no body and no field. No property body means no breakpoints or validation logic can be used. No field means no default value. Typing "prop [tab][tab]" is the Visual Studio code snippet for producing an automatically implemented property. This can make your code much more concise. A classic example of a field back property might look something like the code below.

private int myProperty = 0;public int MyProperty{   get { return this.myProperty; }   set { this.myProperty = value; }}

A simpler example that uses automatically implemented properties for the same field is below.

public int MyProperty{   get; set;}

However, it is important not to be too lazy and use this haphazardly. Examine the code below where you define a Customer class. Take note of the CustomerKey property that is defined. Typically, such a key field is a readonly item that is set once and doesn't change. When taking a shortcut using automatically implemented properties, it allows for undesired behavior with the code.

class Customer{   public string CustomerKey { get; set; }   public string ContactName { get; set; }   public string City { get; set; }}

You might try refactoring the code to have a private set and use a constructor to control the value. Although this does protect the field from the outside, it could still be altered internally to the class.

class Customer{   public Customer(string customerKey)   {      CustomerKey = customerKey;   }   public string CustomerKey { get; private set; }   public string ContactName { get; set; }   public string City { get; set; }}

In reality, an automatically implemented property probably isn't appropriate for the CustomerKey property and the code should look like the example below. The class in question in this example is pretty simple, but it illustrates the point that the automatically implemented properties should be applied liberally, but with caution.

class Customer{   public Customer(string customerKey)   {      this.CustomerKey = customerKey;   }   private readonly string customerKey;   public string CustomerKey   {      get return this.customerKey;   }   public string ContactName { get; set; }   public string City { get; set; }}

using Keyword

There is a prior article that is dedicated solely to the using keyword. Even though this isn't necessarily new, it is worth mentioning and at least pointing to that article for more detail. The main benefit is that it can be used to implement try ... finally behavior and offers an automatic cleanup of disposable types. There is a lot of value to be had by implementing this in your code whenever objects such as DataReader are used to ensure that connections will be properly disposed when complete.

using (IDataReader reader = provider.ExecuteReader(dataCmd)){   // ...}




Page 1 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel