non-null reference

N

Nico

Wouldn't it be neat if I can declare a variable (or parameter or return
value) as a non-null reference.
Say I suffix it with '!' to say that it is always a non-null reference and
have the following signature for a method.

public TypeA! GetX(ClassB! p1, ClassC p2)
{
TypeA v1;
//some code
return v1;
}

p1 must be a non-null value, so I don't have to check it and throw an
ArgumentNullException, the compiler will do that, and the user of the method
knows that it must be non-null.
I can be sure that GetX will never return a null, so I dont have to check
the return value for null, before using it for something else.
Also, the compiler can give a warning saying that 'v1 is of TypeA while you
have to return TypeA!, so either declare v1 also as TypeA! or check it's
null value'

something like :
TypeA! x = null,
will give a compiler error.

If a methodX returns TypeA, then for the same x
TypeA! x = methodX()
will raise a compiler warning (which you can elevate to an error if you
like)

This will greatly reduce the number of NullReferenceExceptions.

Of course all of this only applies to reference types :
int! a;
is not useful.

So where to do I forward suggestions like these ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top