7

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){

}

5 Answers 5

3

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) {...}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

This is also implemented in C# 4.0, in Visual Studio 2010 beta 1.
How is DBC different from checking in your method whether the argument is null or not, and throwing an ArgumentNullException f.i. when the argument is null ?
Functionally, it's not really. Syntactically, though, it makes the intention clear from the external interface of the method by decorating it with an appropriate attribute.
1

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.

1 Comment

I use code contracts with my .NET 3.5 / VS2008 work: msdn.microsoft.com/en-us/devlabs/dd491992.aspx
1

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.

1 Comment

Code Contracts library from .NET 4.0 is publicly available for .NET 3.5 and VS2008. msdn.microsoft.com/en-us/devlabs/dd491992.aspx
0

I'm currently working on this topic in C#. .NET has Nullable<T> for value types, but the inverse feature doesn't exist for reference types.

I created NotNullable<T> for reference types, and moved the problem from if's (no more checks for null) to the datatype domain. However, this makes the application to throw exceptions in runtime and not in compile-time, but it's still very useful for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.