C# Tips and Tricks

Welcome to this installment of the .NET Nuts & Bolts column. 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 .NET Nuts & Bolts series 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 in the .NET Nuts & Bolts series 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))
{
   // ...
}

Local Type Inference

Local type inference was also the subject of another prior .NET Nuts & Bolts article, but again is worth mentioning here as it is very handy to have a solid understanding. You define variables with the var keyword and the compiler will figure out the type for you at compile time and replace it. It is great for results from query expressions as show in the example below. It may appear that it is untyped or even give the impression it is a variant. It is not. It is a strongly typed object whose type is set at compile time.

var i = 5;
int i = 5;

string[] words = { "cherry", "apple", "blueberry" };

var sortedWords =
   from w in words
   orderby w
   select w;

Object and Collection Initializers

Initializers introduce a concise syntax that combines object creation and initialization in a single step. It even allows for parentheses to be optional for paramaterless constructors as you'll see in the first line of the example below.

Point p = new Point { X=3, Y=99 };

Student myStudent = new Student() {
   Name   = "John Doe",
   Class  = "Senior",
   School = "Carmel" };

This functionality is great for demos where you are building collections of objects to do things like LINQ queries against. However, I caution you to consider real life initialization of large sets of data and whether code over a database is the proper place to store such information.

Initializers work very well with constructors. The example below shows an instance where a parameterized constructor is called along with remaining properties initialized. The constructor should commonly take all of the parameters so it isn't an ideal setup, but it illustrates the point well.

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

var c = new Customer("MARKS"){
   ContactName = "Mark Strawmyer",
   City = "Indianapolis"
}

C# Tips and Tricks

Extension Methods

Extension methods allow you to create new functionality on existing types. Even if the types are sealed. They are declared like static methods and called like instance methods. They are valid for use on interfaces or constructed types. The trailing sample code illustrates the use of extension methods to add a Triple and Half method on the decimal type.

static class ExtensionSample
{
   public static decimal Triple( this decimal d ) { return d*3; }
   public static decimal Half( this decimal d ) { return d / 2; }
}

decimal y = 14M;
y = y.Triple().Half();

Extension methods can be a very powerful tool. They can also be a very annoying tool if not used with some care. You should consider a separate namespace to allow them to be optional to the other developers that may be working within your codebase. Resist the temptation to put them on all objects by extending the object class. For consistency you should also make them behave like other instance methods.

The following code sample illustrates very bad form that should not be practiced. The namespace is System, which means the scope of this extension is any code that comes in contact with it. It extends ALL objects. It also does not behave like a normal instance method. Normally calling an instance method on a null object would result in a NullReferenceException and not a Boolean true or false value. This is a code example of what is not advisable.

namespace System
{
   public static class MyExtensions
   {
      public static bool IsNull(this object o) {
         return o == null;
      }
   }
}

while (!myObject.IsNull()) { ... }

LINQ Performance Tip

I'm not going to cover much on LINQ here as those are articles in and of themselves. Here is a LINQ performance tip that I experienced first hand on a project. Consider carefully the use of Table.ToList().Find(). Table.Where() is commonly a better option. ToList().Find() returns all records from a table and then runs the filter in memory. Table.Where() is performed in SQL and returns only the rows requested.

An additional tip is there is a handy tool known as LINQPad that allows you to perform interactive queries of databases using LINQ. It is a code snippet IDE that will execute any C# expression or statement block, which is very handy for testing out LINQ.

Productivity Gains with the Visual Studio C# IDE

The Visual Studio IDE offers a vast number of productivity gains when you have the IDE configured with a C# developer profile.

  • Coding shortcuts
  • Clipboard ring
  • Code snippets
  • Visual Studio 2008 Service Pack 1

Coding Shortcuts

Visual Studio is chalk full of hundreds of coding shortcuts. I'll call out a few of the ones that I've found to be most helpful.

  • Incremental search: Ctrl + I to begin incremental search. You'll notice the mouse pointer will change to binoculars with a downward arrow. Start typing to find a match within the current code file. Ctrl + I again will find the next occurrence in the code file. Ctrl + Shift + I will reverse direction and search upward in the code file.
  • Repeat search: F3 to search again for the last item in the Find dialog box. Ctrl + F3 will behave similarly except will include the Match case option.
  • Commenting and uncommenting code: Ctrl + K followed by Ctrl + C will comment out the current line or selected block of code. Ctrl + K followed by Ctrl + U will uncomment the current line or selected block of code.
  • Indenting: Ctrl + K followed by Ctrl + F will cause Visual Studio to automatically format your code according to the editor formatting rules.

Karen Liu's blog (Microsoft's C# IDE Program Manager) has many, many more useful shortcuts available.

Clipboard Ring

Have you experienced a scenario when you have you cut or copied a line of code only to discover another line of code that you also cut or copy? The original code copied is then lost from the copy buffer and you must undo changes to get back to the first change. The clipboard ring can be your new best friend if this is a common occurrence for you. You cut or copy multiple lines of code independently. Ctrl + Shift + V will allow you to cycle through the code in the clipboard ring. If you are going to use the clipboard ring it is a good idea to consider Tools > Options > Text Editor > All Languages > General and set the "Apply Cut or Copy commands to blank lines when there is no selection" option.

Code Snippets

Code snippets are ready-made task-oriented blocks of code. Visual Studio includes a number of default snippets that are activated by typing a specific sequence of keys followed by [tab][tab] to cause the Visual Studio IDE to engage. In addition to typing the snippet in code, you can right-click at the desired location and choose Insert Snippet or Surround With. Here is a very small sampling of what is available.

  • Tryf = try and finally block
  • Prop = property with get and set accessor
  • Switch = switch statement with default

Microsoft has a number of additional code snippets available for download. Additionally, there is a free Snippet Designer that allows you to easily create or modify your own code snippets.

Visual Studio 2008 Service Pack 1

Visual Studio 2008 Service Pack 1 is full of all kinds of goodies that make it worth downloading and installing. The download will take a while, but definitely worth it. Here is a very limited taste of what is included.

  • Solution-wide TODO tasks.
  • Compiler error "squiggles" on the fly when editing.
  • Step into specific that allows you to step in to a particular method call when you have code that has several nested calls as a part of a single statement.
  • Many, many, more

Summary

You have now seen various tips and tricks related to items within C# 3.0 and the Visual Studio 2008 C# IDE. Hopefully this has provided you some useful insight in to ways you can enhance your C# development experience.

Future Columns

The topic of the next column is likely to be on the Entity Framework. We'll explore what it is and how it compares to LINQ. If you have something else in particular that you would like to see explained here you could reach me at mark.strawmyer@crowehorwath.com.

About the Author

Mark Strawmyer is an executive with Crowe Horwath LLP in the Indianapolis office. He can be reached at mark.strawmyer@crowehorwath.com.



About the Author

Mark Strawmyer

Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. He specializes in architecture, design and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mark.strawmyer@crowehorwath.com.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds