Use of nullable pointers

Apr 10, 2008 at 5:19 AM
It appears after some limited testing that nullable pointers can't be used unless the compiler can prove that they are not null. Basically run-time null-pointer detection is not supported and possibly null pointers generate an error not a warning at compile time. As a result it seems that nullable member variables are not directly usable because they cannot be proved to be non-null in general due to concurrency issues. Is this true? Can they be locked in some way, tested for null and then used? I assume just making a local copy on the stack, testing it and using it would work...

My main question is, what is the point of this nullable pointer error? Why not just do run-time testing like in normal C#? The end result is not to avoid the run-time check, but just to make the user code it by hand. I've found that in every place I need to use a nullable pointer I just test for null first and throw an exception if it is null, just like a compiler generated run-time check would do.
Coordinator
May 8, 2008 at 11:54 PM

Krusty wrote:
It appears after some limited testing that nullable pointers can't be used unless the compiler can prove that they are not null. Basically run-time null-pointer detection is not supported and possibly null pointers generate an error not a warning at compile time. As a result it seems that nullable member variables are not directly usable because they cannot be proved to be non-null in general due to concurrency issues. Is this true? ...What is the point of this nullable pointer error? Why not just do run-time testing like in normal C#? The end result is not to avoid the run-time check, but just to make the user code it by hand.


Hi Krusty. If you're receiving errors when attempting to use nullable member variables it's not because of the possibility of concurrency issues; the type system does not detect this type of error. To insert a runtime null check without explicitly writing one, you can "cast away the nullable" using (!), as in:

Foo f;
Foo! f2 = (!)f;

Generally, in code that makes wide use of non-null pointers, this type of conversion will not be frequently needed; most member variables can use non-null pointers. Let me know if you have any more questions.
--
Derrick Coetzee
Microsoft Research Operating Systems Group developer