views:

143

answers:

2

Visual Studio seems to complain when I pass a string into an exception parameter.

if (str1 == null || str2 == null)
{
    throw new ArgumentNullException("lmkl");
}

Visual Studio says that it cannot resolve symbol "lmkl".

If I have a string variable (eg above throw new... string s = "test";) and include this as the parameter for the exception, Visual Studio is more than happy with this.

What gives?

Thanks

+6  A: 

The documentation for the overloaded constructor for ArgumentNullException that takes a single string parameter states that this argument should be:

The name of the parameter that caused the exception.

At the moment, if your code throws an exception you won't know which argument was null.

Recommend rewriting to

if (str1 == null) throw new ArgumentNullException("str1");
if (str2 == null) throw new ArgumentNullException("str2");
Richard Ev
That doesn't make sense because I can write string s = "anything"; and pass it in as a parameter.
dotnetdev
Thanks for the tip. I'll give that a go! Thanks again
dotnetdev
+6  A: 

Actually, Visual Studio doesn't care about this at all. I assume you have ReSharper installed? This validates a lot of common errors, including incorrect use of patterns such as ArgumentException etc. It also has better null checking - not quite "contracts", but still pretty helpful.

It only attempts this when it can see a string literal used in a known pattern - the analysis to chase how you assign variables is simply too much for realistic analysis.

Marc Gravell
Yep I use resharper. Haven't seen it do its stuff with exceptions in my code, other than that it's been a good tool to work with. Thanks.
dotnetdev