switch(nullable type)

  • Thread starter Thread starter John Grandy
  • Start date Start date
J

John Grandy

Is it considered good practice to perform a switch operation on nullable
types ?
 
Is it considered good practice to perform a switch operation on nullable
types ?

It seems fairly reasonable to me. There are those who dislike switch in
any situation, and there are certainly times where polymorphism
provides a better solution to the same problem, but there are other
times when switch is the cleanest solution - and I can see how
nullables could create such a situation just as easily as non-
nullables.
 
John Grandy said:
Is it considered good practice to perform a switch operation on nullable
types ?

I don't think you can. Each case of a switch statement has to be a literal
value, and those are limited to fundamental integral types, char, and
string.
 
Ben Voigt said:
I don't think you can. Each case of a switch statement has to be a literal
value, and those are limited to fundamental integral types, char, and
string.

I wasn't sure, so I tried it - and it works fine:

using System;

class Test
{
static void Main()
{
int? i=null;

switch (i)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
case null:
Console.WriteLine("null");
break;
}
}
}

Then I checked the ECMA spec - and it seems this doesn't actually
conform to the spec, without any warning. Naughty MS compiler :) (The
mono compiler originally followed the spec, but then followed MS's
lead, as compatibility with MS appears to be more important to them
than strict ECMA conformity, which is not entirely unreasonable.)
 
Jon Skeet said:
I wasn't sure, so I tried it - and it works fine:

using System;

class Test
{
static void Main()
{
int? i=null;

switch (i)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
case null:
Console.WriteLine("null");
break;
}
}
}

Then I checked the ECMA spec - and it seems this doesn't actually
conform to the spec, without any warning. Naughty MS compiler :) (The
mono compiler originally followed the spec, but then followed MS's
lead, as compatibility with MS appears to be more important to them
than strict ECMA conformity, which is not entirely unreasonable.)

Chalk up one more example of why I hate Java and C#... special magic
functionality for certain library types that you can never duplicate on your
own classes.

Wonder what the IL for that actually looks like.... because it can't be
using a jump table. Must have an if/else for Nullable<>.HasValue and a
switch on Nullable<>.GetValueOrDefault. Presumably it's more efficient for
the if to be embedded in the zero case of the switch, although that's not
intuitive.
 
Chalk up one more example of why I hate Java and C#... special magic
functionality for certain library types that you can never duplicate on your
own classes.

While I see your point, there's something to be said for having useful
functionality that could easily be abused if you could do it
everywhere.
Wonder what the IL for that actually looks like.... because it can't be
using a jump table. Must have an if/else for Nullable<>.HasValue and a
switch on Nullable<>.GetValueOrDefault. Presumably it's more efficient for
the if to be embedded in the zero case of the switch, although that's not
intuitive.

It's odd - a call to GetValueOrDefault, then a call to HasValue,
*then* the "if" with an embedded switch.

Strange.

Jon
 

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

Similar Threads


Back
Top