Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Specifying method parameter is not nullable

Tags:

c#

How would I go about specifying that a particular parameter of my method is not nullable without putting in my own exceptions within the method itself?

Is there something like,

public void Foo (String myRequiredString nullable){

}
like image 477
Craig Bovis Avatar asked Jul 17 '09 12:07

Craig Bovis


4 Answers

You can't, basically. You could perhaps use AOP (such as PostSharp) to do some of it for you via attributes, or code-contracts in 4.0 (allowing compile-time checks); but there is no "non-nullable reference-type" (to compare to the "nullable value-type" etc). Not least: what would fields initialize to? and what would default(...) be?

There have been requests to include something in the langauge to help (essentially doing the null check for you), but it hasn't happened. Something like:

public void Foo (string! myRequiredString nullable) {...}
like image 200
Marc Gravell Avatar answered Nov 12 '22 23:11

Marc Gravell


Nothing built in, but you could look at the concept of "design by contract", as implemented in (for example) LinFu.

like image 39
David M Avatar answered Nov 12 '22 22:11

David M


In C# 4.0 you can use Code Contracts: InfoQ: .NET 4 Feature Focus: Code Contracts.

All other solutions at the moment involves runtime code checks. You can use an AOP framework, like PostSharp to inject the code into the method, but it all comes down to code in the method.

like image 1
Lasse V. Karlsen Avatar answered Nov 12 '22 23:11

Lasse V. Karlsen


Aside from getting into some beta version of .NET... which, while exciting, is risky and doesn't really answer your question.

For now unfortunately the best thing you can do is add Intellisense comments to your code that warn not to pass nulls.

    /// <summary>
    /// This Foo method does Bar to something.
    /// </summary>
    /// <param name="myRequiredString">required, do NOT pass nulls. I really really mean it!</param>
    public void Foo(String myRequiredString)
    {
        if (myRequiredString == null)
        {
            throw new ArgumentNullException("myRequiredString", "It said required in the name of the argument, dummy!");
        }
    }

There are a few hackish solutions out there where people have implemented a generic NonNullable struct, but the bottom line is what you want is for the Visual Studio to not let you type "Foo(null)" and to warn you if you do or give you some compiler error, and the fact is for 99.99% of method calls where something shouldn't be nullable, you're going to have to do the == null check and throw an ArgumentNullException anyhow, because you don't know that the argument is null until runtime. Even if there was what your looking for in C# at the very least the compiler will have to add the null check and throw ArgumentNullException in there.

I guess I'm saying is what you're looking for is "syntactic sugar" to save your fingers some movement. For now if you're sick of typing that I'd recommend creating a Code Snippet.

like image 1
Ben Lesh Avatar answered Nov 12 '22 23:11

Ben Lesh