Slicker call to Dispose method

by Miha Markič 15. April 2009 15:19

Do you ever get tired of code like this:

if (someObject != null)
{
someObject.Dispose();
}
or its shorter version
if (someObject != null)
someObject.Dispose();

Isn’t annoying to write those if (something != null) lines? Here is an extension method that does a better job:

public static class IDisposableExtensions
{
  public static void DisposeAlways(this IDisposable disposable)
{
    if (disposable != null)
disposable.Dispose();
}
}
No more ifs. Just write it like:
someObject.DisposeAlways();

DisposeAlways isn’t a perfect name though. Does anybody have a better suggestion? I can’t name it Dispose because it would conflict with existing method.

Tags:

.net 3.5

Comments (10) -

Miki Watts
Miki Watts Israel
4/16/2009 2:31:46 AM #

how about SafeDispose? I usually add the prefix Safe when I create functions that are designed to handle cases like this.

Reply

Miha Markic
Miha Markic Slovenia
4/16/2009 4:29:03 PM #

SafeDispose sounds better indeed.

Reply

David Vidmar
David Vidmar Slovenia
4/16/2009 5:12:08 PM #

I would also go for SafeDispose.

Reply

Matija Lah
Matija Lah Slovenia
4/16/2009 9:31:06 PM #

The way they usually do it at MS the only option is Dispose2.
Smile

Reply

Miha
Miha Slovenia
4/18/2009 3:52:59 AM #

DisposeNow

SafeDispose -- there's nothing safe about it, really. Smile

Reply

Milan Negovan
Milan Negovan United States
5/3/2009 9:01:02 AM #

Miha, how about an even shorter variation?

using (someObject as IDisposable)
{
}

If someObject is null in the first place, the compiler bails (see more here: aspnetresources.com/.../...dy_using_statement.aspx)

Reply

Miha Markic
Miha Markic Slovenia
5/3/2009 5:02:47 PM #

@Milan: That's an interesting using's side effect indeed. Good to know. I find my way cleaner though. Writing "using (x as IDisposable) {}" is more writting and the intention is less obvious - that's my subjective thinking.

Reply

Milan Negovan
Milan Negovan United States
5/6/2009 8:30:16 AM #

Alright, alright, you win. Smile I dig "using" with two shovels, though.

Reply

Miha Markic
Miha Markic Slovenia
5/6/2009 12:45:17 PM #

Haha, I have to win, it is my blog :-P. Anyway, there is another advantage for you: you don't have to include any additional namespace. That wouldn't be a problem if VS could add extension method's namespace automatically as it does for normal classes - Shift-Alt+F10.

Reply

Klemen K.
Klemen K. Slovenia
5/20/2009 1:49:28 PM #

Hi Miha!

How about DisposeMe()? Smile

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS