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

switch and nullable type in C# 2.0

P: n/a
Hi

VS 2005 beta 2 successfully compiles the following:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1 {
class Program {
enum A { a, b, c }
static void Main(string[] args) {
A? aaaa = null;
switch (aaaa) {
case null:
Console.WriteLine("null");
break;
default:
Console.WriteLine("def");
break;
}
}
}
}

Any comments on nullables in switch?

Thank you
Yuriy
Nov 17 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Yuriy Solodkyy wrote:
Any comments on nullables in switch?


I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #2

P: n/a
Hi,

Don't see any problems with that...

Regards - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
Hi

VS 2005 beta 2 successfully compiles the following:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1 {
class Program {
enum A { a, b, c }
static void Main(string[] args) {
A? aaaa = null;
switch (aaaa) {
case null:
Console.WriteLine("null");
break;
default:
Console.WriteLine("def");
break;
}
}
}
}

Any comments on nullables in switch?

Thank you
Yuriy

Nov 17 '05 #3

P: n/a
hi,

which types are allowed in switch? string and integral types?
or any type, if there is only one way of implicit convertion to any of the
types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find anything
where it is said that C# 2.0 enhances a list of types allowed in switch.

yuriy
Yuriy Solodkyy wrote:
Any comments on nullables in switch?

I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm

Nov 17 '05 #4

P: n/a
Yuriy Solodkyy wrote:
which types are allowed in switch? string and integral types? or any
type, if there is only one way of implicit convertion to any of the types
mentioned above.

Nullable<> is neither string nor integral type, and I cannot find anything
where it is said that C# 2.0 enhances a list of types allowed in switch.


Here's what the specs say (C# 1.2):

The governing type of a switch statement is established by the switch
expression. If the type of the switch expression is sbyte, byte, short,
ushort, int, uint, long, ulong, char, string, or an enum-type, then that
is the governing type of the switch statement. Otherwise, exactly one
user-defined implicit conversion (§?6.4) must exist from the type of the
switch expression to one of the following possible governing types: sbyte,
byte, short, ushort, int, uint, long, ulong, char, string. If no such
implicit conversion exists, or if more than one such implicit conversion
exists, a compile-time error occurs.

The constant expression of each case label must denote a value of a type
that is implicitly convertible (§?6.1) to the governing type of the switch
statement. A compile-time error occurs if two or more case labels in the
same switch statement specify the same constant value.

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #5

P: n/a
What is Nullable<A> in this list? Do you know any implicit convertions from
A? to any of the types listed in spec?

yuriy

Yuriy Solodkyy wrote:
which types are allowed in switch? string and integral types? or
any type, if there is only one way of implicit convertion to any of
the types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types
allowed in switch.

Here's what the specs say (C# 1.2):

The governing type of a switch statement is established by the switch
expression. If the type of the switch expression is sbyte, byte,
short, ushort, int, uint, long, ulong, char, string, or an enum-type,
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit conversion (§?6.4) must exist from
the type of the switch expression to one of the following possible
governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion exists, or if more than
one such implicit conversion exists, a compile-time error occurs.

The constant expression of each case label must denote a value of a
type that is implicitly convertible (§?6.1) to the governing type of
the switch statement. A compile-time error occurs if two or more case
labels in the same switch statement specify the same constant value.

Oliver Sturm

Nov 17 '05 #6

P: n/a
Yuriy Solodkyy wrote:
What is Nullable<A> in this list? Do you know any implicit convertions
from A? to any of the types listed in spec?


A Nullable<T> can be used in a switch statement if T itself has an
implicit conversion to one of the listed types, or is a listed type. In
your case, that's the Enum. If you try your sample with a Nullable<T>
where T is an arbitrary class, you'll see that the compiler complains
about it, even if you're still only checking for the null case.

I wonder why you're wondering about this so much... to my mind, this
construct behaves just like one would think it should, doesn't it? IOW, if
a type T can be used in a switch, then T? can be used as well, and can
additionally be checked for the null case. Makes a lot of sense to me.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #7

P: n/a
Nullable<Snum> is not enum itself. It is just value type, which has no implicit
conversion to niether the enum or listed type. so, according to your explanation
in cannot be used in switch, but it works.
Yuriy Solodkyy wrote:
What is Nullable<A> in this list? Do you know any implicit
convertions from A? to any of the types listed in spec?

A Nullable<T> can be used in a switch statement if T itself has an
implicit conversion to one of the listed types, or is a listed type.
In your case, that's the Enum. If you try your sample with a
Nullable<T> where T is an arbitrary class, you'll see that the
compiler complains about it, even if you're still only checking for
the null case.

I wonder why you're wondering about this so much... to my mind, this
construct behaves just like one would think it should, doesn't it?
IOW, if a type T can be used in a switch, then T? can be used as well,
and can additionally be checked for the null case. Makes a lot of
sense to me.

Oliver Sturm

Nov 17 '05 #8

P: n/a
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong, char,
string, or an enum-type,

then that is the governing type of the switch statement. Otherwise, exactly
one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short, ushort,
int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if more
than one such implicit conversion

operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?

The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
hi,

which types are allowed in switch? string and integral types? or any
type, if there is only one way of implicit convertion to any of the types
mentioned above.

Nullable<> is neither string nor integral type, and I cannot find anything
where it is said that C# 2.0 enhances a list of types allowed in switch.

yuriy
Yuriy Solodkyy wrote:
Any comments on nullables in switch?

I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm


Nov 17 '05 #9

P: n/a
Yuriy Solodkyy wrote:
Nullable<Snum> is not enum itself. It is just value type, which has no
implicit conversion to niether the enum or listed type. so, according to
your explanation in cannot be used in switch, but it works.


I wasn't saying that Nullable<Enum> is an Enum. I'm sure the conversion
that's taking place is actually covered by paragraph 24.2 of the C# 2.0
specs - read it if you like, I guess the relevant point for your sample is
the last one in 24.2.2:

If the nullable conversion is from S? to T, the conversion is evaluated as an unwrapping from S? to S followed by the underlying conversion from S to T.

If you decompile a program similar to your sample, you'll see that the
compiler creates calls to the GetValueOrDefault method of the Nullable<T>,
before the comparison itself takes place. This allows for constructs like

int? foo = 42;
if (foo == 42)
...

Or of course for the use of the Nullable<T> in a switch statement, such as
we were discussing.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #10

P: n/a
How does it cover nullables? SomeEnum type is not base type for Nullable<SomeEnum>
and no impllicit conversion exists from Nullable<A> to A.
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong,
char,
string, or an enum-type,
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short,
ushort, int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if
more than one such implicit conversion

operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?

The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
hi,

which types are allowed in switch? string and integral types? or
any type, if there is only one way of implicit convertion to any of
the types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types
allowed in switch.

yuriy
Yuriy Solodkyy wrote:

Any comments on nullables in switch?

I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm

Nov 17 '05 #11

P: n/a
In this case 42 is converted to int?, but it does not explain how switch
works. Specifications says that it must be either listed type or implicit
conversion to the listed type must exist.

Yuriy Solodkyy wrote:
Nullable<Snum> is not enum itself. It is just value type, which has
no implicit conversion to niether the enum or listed type. so,
according to your explanation in cannot be used in switch, but it
works.

I wasn't saying that Nullable<Enum> is an Enum. I'm sure the
conversion that's taking place is actually covered by paragraph 24.2
of the C# 2.0 specs - read it if you like, I guess the relevant point
for your sample is the last one in 24.2.2:

If the nullable conversion is from S? to T, the conversion is
evaluated as an unwrapping from S? to S followed by the underlying
conversion from S to T.

If you decompile a program similar to your sample, you'll see that the
compiler creates calls to the GetValueOrDefault method of the
Nullable<T>, before the comparison itself takes place. This allows for
constructs like

int? foo = 42;
if (foo == 42)
...
Or of course for the use of the Nullable<T> in a switch statement,
such as we were discussing.

Oliver Sturm

Nov 17 '05 #12

P: n/a
Hi Octavio,
see comment inline

"Octavio Hernandez" <do****@danysoft.com> schrieb im Newsbeitrag
news:ue**************@TK2MSFTNGP14.phx.gbl...
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong, char,
string, or an enum-type,

a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short, ushort,
int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if more
than one such implicit conversion

There is no user-defined conversion operator from a nullable type (if its
not defined by the user.), right?
operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?
No

The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
hi,

which types are allowed in switch? string and integral types? or any
type, if there is only one way of implicit convertion to any of the types
mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types allowed in
switch.

yuriy
Yuriy Solodkyy wrote:

Any comments on nullables in switch?

I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm



Nov 17 '05 #13

P: n/a
but with VS2005beta2 you can compile it and it works..
Hi Octavio,
see comment inline
"Octavio Hernandez" <do****@danysoft.com> schrieb im Newsbeitrag
news:ue**************@TK2MSFTNGP14.phx.gbl...
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the
switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong,
char,
string, or an enum-type,

a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short,
ushort, int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if
more than one such implicit conversion

There is no user-defined conversion operator from a nullable type (if
its not defined by the user.), right?
operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?

No
The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
hi,

which types are allowed in switch? string and integral types? or
any type, if there is only one way of implicit convertion to any of
the types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types
allowed in switch.

yuriy

Yuriy Solodkyy wrote:

> Any comments on nullables in switch?
>
I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm

Nov 17 '05 #14

P: n/a
> If the nullable conversion is from S? to T, the conversion is evaluated as
an unwrapping from S? to S followed by the underlying conversion from S to T.
The nullable conversion from S? to T is an Explicit conversion.
This allows for constructs like

int? foo = 42;
if (foo == 42)


This is using the lifted == operator, which is covered in 14.2.7 of the ecma
spec.
Nov 17 '05 #15

P: n/a
Sorry, Yura!

Now I understand what you mean. Very interesting!

Regards - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...
but with VS2005beta2 you can compile it and it works..
Hi Octavio,
see comment inline
"Octavio Hernandez" <do****@danysoft.com> schrieb im Newsbeitrag
news:ue**************@TK2MSFTNGP14.phx.gbl...
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the
switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong,
char,
string, or an enum-type,

a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short,
ushort, int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if
more than one such implicit conversion

There is no user-defined conversion operator from a nullable type (if
its not defined by the user.), right?
operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?

No
The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...

hi,

which types are allowed in switch? string and integral types? or
any type, if there is only one way of implicit convertion to any of
the types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types
allowed in switch.

yuriy

> Yuriy Solodkyy wrote:
>
>> Any comments on nullables in switch?
>>
> I'm sure it's me, but I don't understand the question. :-)
>
> Oliver Sturm
>


Nov 17 '05 #16

P: n/a
> but with VS2005beta2 you can compile it and it works..

I cannot find anyplace in the spec that would explain why it compiles. I
think this is an interesting question and I will take another look when I
have more time.

Note that the C# compiler also lets you switch on bool (which is a compiler
bug).
Nov 17 '05 #17

P: n/a
MarkT [developmentor] wrote:
If the nullable conversion is from S? to T, the conversion is evaluated as
an unwrapping from S? to S followed by the underlying conversion from S to T.


The nullable conversion from S? to T is an Explicit conversion.


I would think so, too, but that's nevertheless exactly what the generated
code shows.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #18

P: n/a
> Any comments on nullables in switch?

I just spent an hour looking at every place either nullable or switch
appears in the spec, and I could not find anything that explains the
behavior. However, the spec is pretty tough reading so I could easily have
missed something. I tentatively vote "bug" on this one. You could submit it
as a bug in the Microsoft Product Feedback Center to get some official word
on it.
Nov 17 '05 #19

P: n/a

"MarkT [developmentor]" <Ma****************@discussions.microsoft.com> wrote
in message news:C4**********************************@microsof t.com...
Any comments on nullables in switch?


I just spent an hour looking at every place either nullable or switch
appears in the spec, and I could not find anything that explains the
behavior. However, the spec is pretty tough reading so I could easily have
missed something. I tentatively vote "bug" on this one. You could submit
it
as a bug in the Microsoft Product Feedback Center to get some official
word
on it.


Note that only the latest v2.0 beta version (2.0.50727) includes the latest
Design Change Request made for VS2005 and the CLR, one of these DCR relate
to nullable types, maybe one could try this using this build.
Note that it's allways safe to file an issue to Product Feedback though.

Willy.

Nov 17 '05 #20

P: n/a

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:OG****************@TK2MSFTNGP15.phx.gbl...

"MarkT [developmentor]" <Ma****************@discussions.microsoft.com>
wrote in message
news:C4**********************************@microsof t.com...
Any comments on nullables in switch?


I just spent an hour looking at every place either nullable or switch
appears in the spec, and I could not find anything that explains the
behavior. However, the spec is pretty tough reading so I could easily
have
missed something. I tentatively vote "bug" on this one. You could submit
it
as a bug in the Microsoft Product Feedback Center to get some official
word
on it.


Note that only the latest v2.0 beta version (2.0.50727) includes the
latest Design Change Request made for VS2005 and the CLR, one of these DCR
relate to nullable types, maybe one could try this using this build.
Note that it's allways safe to file an issue to Product Feedback though.


No, it still works in that build. The change, as I undersatnd it, is that
nullable types are boxed into the underlying value instead of the
Nullable<T> value, ie.

int? x = null;
object y = x;

if (x == null)
...

With the change, x == null evaluates to true whereas before it would have
been false. http://blogs.msdn.com/somasegar/arch...11/450640.aspx
covers the change.

The current spec certainly doesn't explain this behavior, so at worst it
would be an extended feature above and beyond the standard since the
behaviour is logical. Hopefuly they wil clear it up however.
Nov 17 '05 #21

P: n/a
Interesting that if you attempt to use integer value in "case" statement,
say 4, you get error message saying that there is no impllicit conversion
to convert 4 to SomeEnum type. So it looks that compiler recognizes just
SomeEnum as governing type for this switch.
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:OG****************@TK2MSFTNGP15.phx.gbl...
"MarkT [developmentor]"
<Ma****************@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
Any comments on nullables in switch?

I just spent an hour looking at every place either nullable or
switch
appears in the spec, and I could not find anything that explains the
behavior. However, the spec is pretty tough reading so I could
easily
have
missed something. I tentatively vote "bug" on this one. You could
submit
it
as a bug in the Microsoft Product Feedback Center to get some
official
word
on it.

Note that only the latest v2.0 beta version (2.0.50727) includes the
latest Design Change Request made for VS2005 and the CLR, one of
these DCR
relate to nullable types, maybe one could try this using this build.
Note that it's allways safe to file an issue to Product Feedback
though.

No, it still works in that build. The change, as I undersatnd it, is
that nullable types are boxed into the underlying value instead of the
Nullable<T> value, ie.

int? x = null;
object y = x;
if (x == null)
...
With the change, x == null evaluates to true whereas before it would
have been false.
http://blogs.msdn.com/somasegar/arch...11/450640.aspx covers
the change.

The current spec certainly doesn't explain this behavior, so at worst
it would be an extended feature above and beyond the standard since
the behaviour is logical. Hopefuly they wil clear it up however.

Nov 17 '05 #22

P: n/a

"Yuriy Solodkyy" <y.************@gmail.com> wrote in message
news:7c**************************@msnews.microsoft .com...
Interesting that if you attempt to use integer value in "case" statement,
say 4, you get error message saying that there is no impllicit conversion
to convert 4 to SomeEnum type. So it looks that compiler recognizes just
SomeEnum as governing type for this switch.


Ya, it appears that the governing type here is being derived directly from
the null type in a logical way, just without any kind of specification to
support it.
Nov 17 '05 #23

P: n/a
It seems to be an oversight in the spec. The behavior is as intended. Thanks
for pointing it out.
Nov 17 '05 #24

P: n/a
"Yuriy Solodkyy" wrote:
but with VS2005beta2 you can compile it and it works..
Hi Octavio,
see comment inline
"Octavio Hernandez" <do****@danysoft.com> schrieb im Newsbeitrag
news:ue**************@TK2MSFTNGP14.phx.gbl...
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the
switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong,
char,
string, or an enum-type,

a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short,
ushort, int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if
more than one such implicit conversion

There is no user-defined conversion operator from a nullable type (if
its not defined by the user.), right?
operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression, right?

No
The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...

hi,

which types are allowed in switch? string and integral types? or
any type, if there is only one way of implicit convertion to any of
the types mentioned above.

Nullable<> is neither string nor integral type, and I cannot find
anything where it is said that C# 2.0 enhances a list of types
allowed in switch.

yuriy

> Yuriy Solodkyy wrote:
>
>> Any comments on nullables in switch?
>>
> I'm sure it's me, but I don't understand the question. :-)
>
> Oliver Sturm
>


Just as Oliver Sturm said, it works just as it should, so what's the
problem? I don't see any. Yes, may be documentation is not quite right in
this case, but what do you want? You want to change compiler so, that you'll
have to write explicit comversion every time? I don't think that would be
right decision.
Nov 17 '05 #25

P: n/a
Vitaliy Kourokhtin wrote:
Just as Oliver Sturm said, it works just as it should, so what's the
problem? I don't see any. Yes, may be documentation is not quite right in
this case, but what do you want? You want to change compiler so, that
you'll
have to write explicit comversion every time? I don't think that would be
right decision.


Just a short comment, as you're talking about what I said: of course the
implementation makes sense the way it works - I don't think anybody was
contradicting that in this thread. But apparently, as I wasn't immediately
prepared to believe but studious documentation readers made clear, what
the language does is not compliant with the documentation; this should of
course be fixed, hopefully on the side of the documentation. It's
important to find and fix this kind of issue to assure compatibility with
other implementations of the C# language.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #26

P: n/a
I just tried to understand how compiler handles it, as it seemed to me that
it should produce error accroding to specification.
"Yuriy Solodkyy" wrote:
but with VS2005beta2 you can compile it and it works..
Hi Octavio,
see comment inline
"Octavio Hernandez" <do****@danysoft.com> schrieb im Newsbeitrag
news:ue**************@TK2MSFTNGP14.phx.gbl...
Yuri,

Quoting the C# Specification, 3rd Edition (p. 228):

"The governing type of a switch statement is established by the
switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong,
char,
string, or an enum-type,
a nullable type is non if this types, right?

then that is the governing type of the switch statement. Otherwise,
exactly one user-defined implicit

conversion operator (§13.4) shall exist from the type of the switch
expression or a base type of this type to

one of the following possible governing types: sbyte, byte, short,
ushort, int, uint, long, ulong,

char, string. If no such implicit conversion operator exists, or if
more than one such implicit conversion

There is no user-defined conversion operator from a nullable type
(if its not defined by the user.), right?

operator exists, a compile-time error occurs."

This covers the case of a nullable type as switch expression,
right?

No

The standard is at www.ecma-international.org.

Privet - Octavio

"Yuriy Solodkyy" <y.************@gmail.com> escribió en el mensaje
news:7c**************************@msnews.microsoft .com...

> hi,
>
> which types are allowed in switch? string and integral types?
> or any type, if there is only one way of implicit convertion to
> any of the types mentioned above.
>
> Nullable<> is neither string nor integral type, and I cannot find
> anything where it is said that C# 2.0 enhances a list of types
> allowed in switch.
>
> yuriy
>
>> Yuriy Solodkyy wrote:
>>
>>> Any comments on nullables in switch?
>>>
>> I'm sure it's me, but I don't understand the question. :-)
>>
>> Oliver Sturm
>>

Just as Oliver Sturm said, it works just as it should, so what's the
problem? I don't see any. Yes, may be documentation is not quite right
in this case, but what do you want? You want to change compiler so,
that you'll have to write explicit comversion every time? I don't
think that would be right decision.

Nov 17 '05 #27

P: n/a
Vitaliy Kourokhtin <Vi***************@discussions.microsoft.com> wrote:
Just as Oliver Sturm said, it works just as it should, so what's the
problem? I don't see any. Yes, may be documentation is not quite right in
this case, but what do you want? You want to change compiler so, that you'll
have to write explicit comversion every time? I don't think that would be
right decision.


I think "what you want" is for the documentation to match the
implementation. Now, that allows for the case where you correct the
documentation to match the implementation, as here.

What's the point of a specification if it's not accurate?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.