By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

non-null reference

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.