0

Is there anyway of avoiding some passing in null into a method.

Something like:

   public static string Add([not null] string word1 , string word2)
        {
            return word1 + word2;
        }
![enter image description here][1]
string sentence = Add(null, "world");
                       ^^ compile error
Yagnesh Cangi
  • 393
  • 3
  • 17
  • 2
    http://stackoverflow.com/questions/2923480/what-is-a-practical-usage-of-code-contracts-in-net-4-0 or http://www.nudoq.org/#!/Packages/Sasa/Sasa/NonNull(T) – Mauricio Scheffer Mar 27 '15 at 10:14
  • You can let the user pass it and check if null – mayowa ogundele Mar 27 '15 at 10:15
  • You should handle that case in `Add` with `if(word1 == null) throw new ArgumentNullException("word1");`. How could the compiler know if an object is `null` at compile time? instead of `Add(null, "world");` it could also be `Add(maybeNullOrNot, "world");`. – Tim Schmelter Mar 27 '15 at 10:17

3 Answers3

4

No. The compiler isn't psychic. There are situations where the compiler simply wouldn't be able infer what will be passed ahead of time.

Consider the following and how the compiler might possibly protect you...

var rnd = new Random();
Foo(rnd.Next(2) == 0 ? "foo" : null);
spender
  • 117,338
  • 33
  • 229
  • 351
  • What about using a custom [Code Fix](https://msdn.microsoft.com/en-us/magazine/dn879356.aspx) on Visual Studio 2015 with Roslyn? It shall not be at compile time, but I think it could be possible to detect before that. – HuorSwords Mar 27 '15 at 10:25
  • @HuorSwords: what is the benefit of a runtime check against simply checking it yourself with `if(word1 == null) throw new ArgumentNullException("word1");`? That code is self-documenting and should be the first in the method. – Tim Schmelter Mar 27 '15 at 10:26
  • I was just thinking, the compiler knows it's expecting a string... there should be a way of know it's a string which cannot be null – Yagnesh Cangi Mar 27 '15 at 10:28
  • @TimSchmelter I don't know. :) Perhaps the OP will have a solid reason to want to define a method in that way... – HuorSwords Mar 27 '15 at 10:29
  • 1
    @YagneshCangi: i don't understand why you don't understand the `Random` example in this answer, isn't it clear? The compiler cannot know in any case if the argument is null. Of course passing `null` is clearly a `null`. But the argument normally gets it's value at runtime. – Tim Schmelter Mar 27 '15 at 10:31
  • Question came to my head when I was doing.... `AppDomain.CreateInstanceFromAndUnwrap(` It was expecting `[NotNull] string assebmltName` – Yagnesh Cangi Mar 27 '15 at 10:33
  • 4
    In this case it's either Foo("foo") or Foo(null) a compiler can easily detect both scenario's. – Wouter Feb 20 '18 at 22:56
1

There is no way to check it on compile time as string is reference type. The common and useful thing is to check on runtime and throw ArgumentNullException. The user of this method will catch exception as soon as he will use it in wrong way.

For example:

public static string Add(string word1, string word2)
{
    if (word1 == null) throw new ArgumentNullException("word1");

    return word1 + word2;
}
0

If you would like to reach similar syntax to your example you could use PostSharp Code Contracts. Free version allows you to use such attribute up to 10 classes per project, check the examples PostSharp Code contracts examples. It won't give you compiller error, but will simplify the code.

Dwiks
  • 1