views:

76

answers:

2

It's often the case when I need to check a method's arguments for not being null, something like:

public static bool operator <= 
    (ApplicationVersion i_app1, ApplicationVersion i_app2)
{ 
    if (i_app1 == null) throw new ArgumentNullException("i_app1");
    if (i_app2 == null) throw new ArgumentNullException("i_app2");
    [...]
}

Is there a way to do this (semi)automatically, like Code snippets? Autocomplete? Resharper?

+1  A: 

Resharper has this feature. If you have cursor over parameter name - say i_app1 and hit Alt-Enter - it offers the option to check for null.

Rashack
ok, so I'm officially blind.
Cristi Diaconescu
It's hidden under the lightbulb icon that appears on your left once your cursor is on the parameter - in the method signature. Also what version of R# you use?
Rashack
The "I'm blind" comment was intended as self-irony, i.e. the answer was right there under my nose and I failed to see it until you pointed me to it :).
Cristi Diaconescu
+1  A: 

I have an extension method for this which at least makes it slightly simpler:

i_app1.ThrowIfNull("i_app1");

The implementation is simple:

public static void ThrowIfNull<T>(this T argument, string name)
    where T : class
{
    if (argument == null)
    {
        throw new ArgumentNullException(name);
    }
}

I prefer this to using snippets or whatever, as it means the code itself is shorter. Typing is rarely an issue for me - whereas keeping the code as readable as possible is.

Hopefully Code Contracts in .NET 4.0 will make this neater still though.

Jon Skeet
This type of extension method makes me shudder a bit. The only thing more disturbing is ByRef extension methods :). http://blogs.msdn.com/jaredpar/archive/2008/12/11/extension-methods-and-byref-targets.aspx
JaredPar
@Jared: What do you dislike about it? It says what you intend to do incredibly simply. As for the "by ref" one - presumably that doesn't work in C#? I'd really hope not!
Jon Skeet
My gut reaction is to balk at a method that is called on a null reference. I realize it's an extension method and under the hood it becomes a static call so no issue. But I still have a hard time getting over the initial gut reaction.
JaredPar
(cont) I personally prefer ThrowIfNull(i_app1, "i_app1") but that's not feasible in C# (works great in VB). In C# i usually go for Argument.VerifyNotNull(i_app1,"i_app1") or go for a snippet.
JaredPar
(cont2) and yes, byref extension methods are not supported in C#.
JaredPar
I think it's a matter of just realising that it *does* support null references - I think I'd only use that fact for methods which are *explicitly* to do with null, e.g. this, IsNullOrEmpty etc.
Jon Skeet
@Jon, very true. I think i've been ruined because of several bits of C++ code I've worked with which have explicitly different behavior when an instance method is called with a NULL this. No special method names, just "oh no problem, we support NULL in a weird way" type methods.
JaredPar
Interesting approach. However, I'm bound to .net 2.0, so I can't use extension methods :(
Cristi Diaconescu
@Cristi, you can use extension methods in 2.0 app as long as you are using a C# 3.0 compiler (http://blogs.msdn.com/jaredpar/archive/2007/11/16/extension-methods-without-3-5-framework.aspx)
JaredPar