How to Implement IDisposable (the easy way)

by Chad 2. April 2012 07:48

For my friends in the Springfield .NET Users group. You can use this C# code snippet to quickly implement the Dispose pattern in your classes. Enjoy!

How to install the snippet

Drop this .snippet file in your C:\Users\<UserName>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets folder.

How to use this snippet

 

  1. Add the IDisposable interface to your class
  2. With the cursor inside your class declaration type: dispimp + tab + tab

 

Download the Snippet

dispimp.snippet (1.50 kb)

Resources

http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Tags:

.NET-Basics

Catching Common Exception Handling Mistakes

by Chad 18. January 2011 11:10

Microsoft provides excellent guidance for exceptions, but it's easy to forget and make some of these common exception handling mistakes. Below is a list of "must remember" exception handling guidance when programming in C# (although most of this is also applicable to other .NET languages.)

When to Catch Exceptions?

Only catch an exception when you understand exactly why it will be thrown and can recover from it.

If you follow this one rule, it will prevent many of the problems that you can get into when handling exceptions.  Most of the time, you can find a list of exceptions that a member will throw from the member's XML documentation. You can access this documentation via Intellisense or by hovering the mouse cursor over the member. For detailed documentation, you can often see a list of exceptions along with the specific condition that will throw the exception on the member's MSDN documentation page.

This may feel funny at first, but remember that exceptions aren't bad. Understanding when and why a specific exception will be thrown will create more stable and secure code. If your application crashes because of an unhandled exception, that's just a signal that you forgot to program for a specific condition. So, before typing “catch” answer both of these questions:

  1. Do I know exactly why this exception is being thrown here?
  2. Can I recover from this exception?

 

Do not catch System.Exception
You should never catch System.Exception (or System.SystemException,) unless you plan to log and immediately re-throw the exception. Because all managed exceptions ultimately inherit from System.Exception, you cannot possibly know exactly why the exception you’re catching is being thrown. You should always catch the most specific exception type that you can.

If you do succumb to the temptation of catching System.Exception and are able to repress the shame from such coding Smile do be sure to use “catch(Exception) {…}” NOT “catch {…}”. A catch statement without an exception type will catch all exceptions, including non-CLS exceptions.

"throw;" != "throw err;"
If you catch an exception for logging or other reasons, but then need to re-throw the exception, ALWAYS use "throw;". "throw err;" will affect the stack trace making troubleshooting more difficult. Look at the following call stacks and sample code for an example of the problem that "throw err;" causes.

// With throw err;
//at MyApp.Program.Method1() in ...\Program.cs:line 29
//at MyApp.Program.Run() in ...\Program.cs:line 18
//at MyApp.Program.Main(String[] args) in ...\Program.cs:line 13
class Program
{
  static void Main(string[] args)
  {
    Program p = new Program();
    p.Run();
  }

  void Run()
  {
    this.Method1();
  }

  void Method1()
  {
    try { Method2(); } catch (Exception err) { throw err; }
  }

  void Method2()
  {
    try { Method3(); } catch (Exception err) { throw err; }
  }

  void Method3()
  {
    throw new InvalidOperationException();
  }
}
// With throw;
//at MyApp.Program.Method3() in ...\Program.cs:line 33
//at MyApp.Program.Method2() in ...\Program.cs:line 28
//at MyApp.Program.Method1() in ...\Program.cs:line 23
//at MyApp.Program.Run() in ...\Program.cs:line 18
//at MyApp.Program.Main(String[] args) in ...\Program.cs:line 13
class Program
{
  static void Main(string[] args)
  {
    Program p = new Program();
    p.Run();
  }

  void Run()
  {
    this.Method1();
  }

  void Method1()
  {
    try { Method2(); } catch (Exception) { throw; }
  }

  void Method2()
  {
    try { Method3(); } catch (Exception) { throw; }
  }

  void Method3()
  {
    throw new InvalidOperationException();
  }
}

Tags:

.NET-Basics

Coding even faster with TemporaryMacro in Visual Studio

by Chad 12. April 2010 10:43

Last week I found my new favorite Visual Studio 2008 productivity tool: TemporaryMacro. You can find this functionality in Tools > Macros > Record/Run TemporaryMacro. Being a shortcut-junkie though, I prefer Ctrl+Shift+R and Ctrl+Shift+P for recording and playing respectively.

The Macro feature in Visual Studio is one of those things that I’ve always been aware of, but have never really taken advantage of because I didn’t want to manage a library of macros. The TemporaryMacro feature is a quick and easy to use “throw away” macro and great for tedious text modifications.

Example Usage
This is a simple example, but yesterday I had pasted a digital certificate thumbprint into a .config file. The thumbprint had a space every two characters, that wasn’t necessary. To remove the spaces, I had to press: Right > Right > Delete for all 19 spaces that I wanted to remove.

With macros I started the recorder by pressing Ctrl+Shift+R. I pressed Right > Right > Delete to remove the first space. I then pressed Ctrl+Shift+R to stop the macro recording. Then I quickly pressed Ctrl+Shift+P 18 more times to replay the macro removing the remaining spaces.

Useful Text Manipulation Shortcuts
The record > replay process takes a tiny bit of forethought because you need to consider how executing the macro repeatedly will behave. You also need to think about the edge case of your final replay as you may accidentally change text that you didn’t intend to change. I use these shortcuts all of the time, but I find that within the context of macros I use them differently to affect the behavior of the text manipulation replays.

Arrow – Move the cursor left or right one character or up or down one line.
Shift+Arrow – Select text while moving cursor moves up, down, left, or right.
Ctrl+Left (or Right) – Move the cursor left or right one word at a time.
Shift+Ctrl+Left (or Right) – Select text while moving left or right one word at a time.
End – Move the cursor to the end of the line.
Ctrl+End – Move the cursor to the end of the file.
Home – Move the cursor to the beginning of text for the current line.
Home (a second time) – Move the cursor to the begging of the line.
Shift+{any of the home/end combinations} – Select text while moving the cursor.
Ctrl+Home – Move the cursor to the beginning of the file.
Shift+Delete – Delete the entire line.

It may feel slow at first, but I promise you that if you stick with it, the TemporaryMacro will become a frequent tool in your daily coding habits.

Tags:

.NET-Basics

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Chad

Meeeee!!!

Hi, my name is Chad Boschert and I'm a software developer in Springfield, Missouri. I've been developing .NET applications in C# since 2002.

Recent Comments

Comment RSS

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar