5

I understand that C# compiler as it stands does not let switching on Type like

switch (typeof(MyObj))
    case Type1:
    case Type2:
    case Type3:

There are solutions where a Dictionary of Type and Action could be used or a static class (link), but I am just curious, why is this a bad practice or not implemented in the compiler yet ?

Thank you in advance.

user207421
  • 305,947
  • 44
  • 307
  • 483
RaM
  • 1,126
  • 10
  • 25

3 Answers3

1

If you're talking about primitive types you can do switch over TypeCode.

var typeCode = Type.GetTypeCode(type);    
switch (typeCode)
{
    case TypeCode.Empty:
        break;
    case TypeCode.Object:
        break;
    case TypeCode.DBNull:
        break;
    case TypeCode.Boolean:
        break;
    case TypeCode.Char:
        break;
    case TypeCode.SByte:
        break;
    case TypeCode.Byte:
        break;
    case TypeCode.Int16:
        break;
    case TypeCode.UInt16:
        break;
    case TypeCode.Int32:
        break;
    case TypeCode.UInt32:
        break;
    case TypeCode.Int64:
        break;
    case TypeCode.UInt64:
        break;
    case TypeCode.Single:
        break;
    case TypeCode.Double:
        break;
    case TypeCode.Decimal:
        break;
    case TypeCode.DateTime:
        break;
    case TypeCode.String:
        break;
}

BTW answer for your question you may need to read How many Microsoft employees does it take to change a lightbulb?

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

I would refer you to the accepted answer for : switch statement in C# and "a constant value is expected" . The compiler needs to know at compile time that there won't be any duplicates, so it accepts only constants. By the way, you can achieve the same effect with

switch (typeof(MyObj).FullName 

and use the names of each type as case conditions, like:

case "MyNamespace.Type1":
   /*stuff*/
   break;
case "MyNamespace.Type2":
   /*other stuff*/
   break;
default:
   /*default stuff*/
Community
  • 1
  • 1
Boluc Papuccuoglu
  • 2,318
  • 1
  • 14
  • 24
1

When would you use switching on type? Most of the cases I can think of are better solved with inheritance, i.e. rather than doing:

switch (typeof(MyObj)) {
    case Type1: doSomethingForType1; break;
    case Type2: doSomethingForType2; break;
    case Type3: doSomethingForType3; break;

You would set it up in a much more object-oriented manner:

Interface ISpecialType {
    void doSomething();
}
Type1 : ISpecialType {
    doSomething() {}
}
Type2 : ISpecialType {
    doSomething() {}
}
Type3 : ISpecialType {
    doSomething() {}
}

then, no matter what, you just call MyObj.doSomething(); It's a little bit more typing at the start, but a lot more robust.

Also, if it's really important to you to switch on, you can always use typeof(MyObj).toString() and switch on that. It's not recommended practice, since you're then hard-coding strings that are allowed to change into your switch, but you can do it.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • 1
    I can what you are saying Scott and I cannot agree more, I was trying to refactor legacy code where diffirent exceptions where thrown and was caught in the parent level, which was then used for sending the apporpiate error message. And I found lots of : if (ex is XmlSchemaValidationException) errorCode = ServiceExceptionErrorCode.XmlSchemaValidation; else if (ex is BadRequestException) errorCode = ServiceExceptionErrorCode.BadRequest; and was not sure how to clean this mess, to be honest. Thanks a lot for your comment anyway. – RaM Nov 12 '13 at 15:31