Coding Standards & Conventions

March 20, 2009 08:28

C Sharp Coding Shortcuts

January 29, 2009 08:01

The C# language as well as the Visual Studio environment contain a number of shortcuts meant to ease our development lives. Here's a few of them.

1. Initializing Variables

Sometimes, it is not necessary to initialize variables because they are already initialized implicitly. For example, writing this is redundant:

   1: bool bFlag = false;

A type of 'bool' is implicitly initialized to 'false', so while adding the "= false" is nice from an explicit standpoint, it is unnecessary.

Further, this is also redundant:

   1: string strTestString = string.Empty;

A 'string' type is implicitly set to empty (though if class scope it will be null, so be careful).

Also,

   1: int i = 0;

Types of 'int' are implicitly set to '0';

2. If…Then

Sure, you can code this:

   1: if (strYear == null)
   2: {
   3:     return ("null");
   4: }
   5: else
   6: {
   7:     return (strYear);
   8: }

But there's a more concise shorthand available from the C++ world that was carried into C#:

   1: (strYear != null)?strYear:"null"

Even more concise is this C# construct:

   1: strYear ?? "null"

A full example might be:

   1: StringBuilder sb = new StringBuilder();
   2: sb.AppendFormat ("Year [{0}]", strYear ?? "null");

3. Use TAB to Add Event Handlers

You can add event handlers by hand, or you can take advantage of the "Press TAB to insert" functionality.

Say you've got a button to which you want to add the "Click" event. Once you've typed the below "b.Click +=" you'll see a little pop-up prompting you to hit TAB to insert.

image

Hitting TAB will get you to here:

image

Hitting TAB again will give you the final code, which includes the event handler itself:

   1: protected void Page_Load(object sender, EventArgs e)
   2: {
   3:     Button b = new Button();
   4:  
   5:     b.Click += new EventHandler (b_Click);
   6: }
   7:  
   8: void b_Click (object sender, EventArgs e)
   9: {
  10:     throw new NotImplementedException ();
  11: }

4. Use var to declare variables (C# 3.0 specific)

From the MSDN documentation:

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

What that means is if you have this:

   1: string strSampleString = "This is a test";

You could alternatively declare it as:

   1: var strSampleString = "This is a test";

This declaration allows the compiler to determine the type behind the scenes, but still at compile time. Also, "var" does not mean "variant", nor is it late-bound.

More information about implicitly typed variables can be found here.

5.) Use string.IsNullOrEmpty

Instead of writing this:

   1: if ( (strId == null) || (strId.Length == 0))
   2: {
   3:     return;
   4: }

Use this:

   1: if (string.IsNullOrEmpty (strLocatorId))
   2: {
   3:     return;
   4: }

References/More Info

MSDN: Default Code Snippets