[262] Nullable enum in switch

Hi,

in C# 2.0, the compiler accepts nullable types for the 'switch'
statement, but R# indicates it as an error:

public enum TestEnum
{
One,
Two
}

public class MyClass{
public void Method()
{
TestEnum? testEnum = TestEnum.One;

switch(testEnum)
{
case TestEnum.One:
break;
case TestEnum.Two:
break;
}
}
}

Best regards,

Wiebe Tijsma

2 comments
Comment actions Permalink

There was discussion on this some time ago.
The key idea is that such construct is directly prohibited by C# lang
specification (see the definition of the switch statement governing type).
Implementation of this issue without C# spec is not so trivial as it seems,
because of conversion rules to governing type

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Wiebe Tijsma" <wiebeREMOVE@CAPITALStijsma.com> wrote in message
news:eev3pd$3b6$1@is.intellij.net...

Hi,

>

in C# 2.0, the compiler accepts nullable types for the 'switch' statement,
but R# indicates it as an error:

>

public enum TestEnum
{
One,
Two
}

>

public class MyClass{
public void Method()
{
TestEnum? testEnum = TestEnum.One;

>

switch(testEnum)
{
case TestEnum.One:
break;
case TestEnum.Two:
break;
}
}
}

>

Best regards,

>

Wiebe Tijsma



0
Comment actions Permalink

Ah, I see, I thought it might be compiler-error worthy anyway.

I think it's a strange design decission to choose a non-nullable type
(struct) as base for a nullable type (class) anyway, as they had to make
some illogical changes to the CLR for this, ("struct type == null"
isn't normally allowed).

Eugene Pasynkov (JetBrains) wrote:

There was discussion on this some time ago.
The key idea is that such construct is directly prohibited by C# lang
specification (see the definition of the switch statement governing type).
Implementation of this issue without C# spec is not so trivial as it seems,
because of conversion rules to governing type

0

Please sign in to leave a comment.