C# interview

Don nospam at nospam.com.au
Mon Oct 6 02:58:39 PDT 2008


Denis Koroskin wrote:
> The two things that needs to be changed to support this feature are:
> 
> 1) make typeof(null) == void*
> 2) remove default initializers (for reference types, at least)
> 
> The latter rule can be relaxed (as done in C#): you can have a variable 
> uninitialized. However, you can't read from it until you initialize it 
> explicitly. This is enforced statically:
> 
> // The following is ok:
> Object o;
> o = new Object();
> 
> // This one is ok, too:
> Object o;
> if (condition) {
>    o = new Foo();
> } else {
>    o = new Bar();
> }
> 
> // But this is rejected:
> Object o;
> if (condition) {
>    o = new Foo();
> }
> 
> Object o2 = o; // use of (possibly) uninitialized variable

Why not just disallow uninitialized references?
So none of the examples would compile, unless you wrote:

Object o = new Object();

or

Object o = null;

The statement that "50% of all bugs are of this type" is consistent with 
my experience. This wouldn't get all of them, but I reckon it'd catch 
most of them.
I can't see any advantage from omitting the "=null;"



More information about the Digitalmars-d mailing list