469,883 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

non-null reference

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 ?
Nov 15 '05 #1
1 1773
In article <#k**************@TK2MSFTNGP09.phx.gbl>, ni**@qmuzik.com
says...
So where to do I forward suggestions like these ?


ms****@microsoft.com

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by lothar | last post: by
3 posts views Thread by Mario | last post: by
25 posts views Thread by Yves Glodt | last post: by
32 posts views Thread by Adrian Herscu | last post: by
22 posts views Thread by Steve - DND | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
12 posts views Thread by puzzlecracker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.