Consider this type, is it as immutable as I can make it?
public struct SomeType
{
public const int OneValue = 1;
private static readonly one = new SomeType(one);
private readonly int value;
private SomeType(int value)
{
this.value = value;
}
public static One
{
get { return this.one; }
}
public static implicit operator int(SomeType source)
{
return source.value;
}
public void SomeSpecialization()
{
}
}
This allows me to do this,
var one = SomeType.One;
switch (one)
{
case SomeType.OneValue:
...
}
but, is there anyway I can remove
public static implicit operator int(SomeType source)
{
return source.value;
}
from the type definition and use the type like this?
var one = SomeType.One;
switch (one)
{
case SomeType.One:
...
}
The
caseexpressions in aswitchstatement can only be compile-time constants of certain built-in types andenums. So the answer is no: no matter what you do with yourSomeType(short of turning it into anenum), you cannot useSomeTypeobjects ascaseexpressions.