c#

Visual Studio Keyboard Tip #5 - Code Snippets

When I first saw code snippets in the Eclipse IDE a few years ago, I thought it was a great feature. It’s nice to see it included in Visual Studio 2005.

Let’s take a quick tour.

  1. Open up visual studio, and go to any C# code editor window.
  2. Position the cursor inside a method and enter CTRL + SPACE

Code Snippet Dropdown

In the dropdown that appears, look at the icon next to the #if or #region — that icon indicates the entry is a code snippet. Ok, next step:

  1. Type for
  2. This should set the selected index of the dropdown to the entry for.
  3. While for is selected enter TAB, then TAB again.

You should see this:

For Code Snippet

The caret is positioned on the i. Type the following word - index. Then hit TAB. You should see that all instances of i are replaced with index. Pretty cool.

Even better, all code snippets are defined in an xml format that you can edit.

  1. Enter the following keys CTRL + K, CTRL + B. This should bring up the code snippet manager.
  2. Expand the Visual C# node, then select for, in the location label right between the Language dropdown and the treeview gives you the location of the xml for the code snippet.
  3. Select and copy the path, then enter CTRL + 0 (that’s not a zero), and paste it into the filename, hit enter, and now you’ll see the xml for the code snippet.

This post on the VSEditor blog provides some details on the schema used to create a code snippet file.

I hope you are enjoying this series of visual studio keyboard tips. If you have any editor/keyboard topics that you would like to see, please feel free to comment on this blog and I’ll try and create a new post covering it. Thanks for reading!

visual studio
keyboard
c#

Comments (0)

Permalink

The NotImplementedException

The NotImplementedException is a very simple, but useful class. It’s intent is to indicate, obviously, that a method is not implemented yet. This is very simple to search for while editing code and also something you can’t miss at runtime.

Okay, let’s give it a try. Open up any C# project, move inside any method, and type a call to a method that you are sure does not exist. For example:

TestNotImplementedException();

Select the above text, right click and choose “Generate Method Stub”. If you were surprised as I was that instead of generating:

throw new NotImplementedException();

Visual Studio generated:

throw new Exception("The method or operation is not implemented.");

Read Anson Horton’s blog post that describes why the decision was made, and how by editing a snippet file you can change the default behavior.

c#
.net

Comments (0)

Permalink

WeakReferences

A WeakReference allows you to hold a reference to an object while allowing the garbage collector to reclaim it.

What ??

Sounds strange at first, but it can be very useful. For example, you could have a rather large object created to present data to a user. You might keep a WeakReference around after you set the “strong reference” to the object to null when the user cancel’s out of the screen.

If the user were to open that screen up again, you could check the Target property of the WeakReference to see if the large object has been garbage collected yet. If the Target property is not null, you can create a new “strong reference” by setting a normal reference type variable to the WeakReference.Target property.

Since there is now a “strong reference” to the object, it will not be reclaimed by the garbage collector.

Here’s a great WeakReference blog post by Brad Abrams, which includes comments from a developer on the Microsoft CLR team.

This article Using Weak References provides details on the difference between a long and short weak reference.

c#
.net

Comments (1)

Permalink

ReaderWriterLock Synchronization Class

The ReaderWriterLock class is a great alternative to other sychronization methods when you have situations where you have infrequent short duration writes, and a high frequency of reads.

This class is designed to allow concurrent access for multiple readers threads, or write access for a single thread.

The msdn help article provides a good example to demonstrate how it can be used.

c#
.net

Comments (0)

Permalink