5

Is there a way to convert the below if else condition into Switch in C#. I am using Equals method for checking the type, but unable to convert to switch case.

if (fi.FieldType.Equals(typeof(int)))
{
    fi.SetValue(data, BitConverter.ToInt32(buffer, 0));
}
else if (fi.FieldType.Equals(typeof(bool)))
{
    fi.SetValue(data, BitConverter.ToBoolean(buffer, 0));
}
else if (fi.FieldType.Equals(typeof(string)))
{
    byte[] tmp = new byte[la.length];
    Array.Copy(buffer, tmp, tmp.Length);
    fi.SetValue(data, System.Text.Encoding.UTF8.GetString(tmp));
}
else if (fi.FieldType.Equals(typeof(double)))
{
    fi.SetValue(data, BitConverter.ToDouble(buffer, 0));
}
else if (fi.FieldType.Equals(typeof(short)))
{
    fi.SetValue(data, BitConverter.ToInt16(buffer, 0));
}

Please help us....

Bhrathi
  • 79
  • 4
  • 8

1 Answers1

11

With C# 7 pattern matching you can do this:

switch (fi.FieldType)
{
    case var _ when fi.FieldType.Equals(typeof(int)):
        fi.SetValue(data, BitConverter.ToInt32(buffer, 0));
        break;
    case var _ when fi.FieldType.Equals(typeof(bool)):
        fi.SetValue(data, BitConverter.ToBoolean(buffer, 0));
        break;

    //etc
}

Note that this is using _ to discard the value as we don't care about it.

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Thanks for quick reply. – Bhrathi Apr 18 '18 at 12:48
  • You could use [Type.GetTypeCode](https://msdn.microsoft.com/en-us/library/system.type.gettypecode(v=vs.110).aspx) and use a switch with earlier versions too – Panagiotis Kanavos Apr 18 '18 at 12:48
  • @Bhrathi this looks like reflection code. What are you really trying to do? There may be an implementation for this already – Panagiotis Kanavos Apr 18 '18 at 12:49
  • @PanagiotisKanavos Yes, that's assuming they're all CLR types of course – DavidG Apr 18 '18 at 12:50
  • @DavidG that's a safe assumption in this case. BitConverter doesn't even support *all* base types – Panagiotis Kanavos Apr 18 '18 at 12:52
  • @PanagiotisKanavos Well possibly safe, but I don't like making assumptions if I can help it. There's no reason why OP may not add a new case in the future which is some DTO for example. – DavidG Apr 18 '18 at 12:54
  • While this is technically using a `switch`, you're still using it as if it were an `if else` chain. `fi.FieldType` in `switch (fi.FieldType)` is unused, you could put literally any value there. You're only using the `when boolean_evaluation` part, which is no different from `if(boolean_evaluation)`. This is one of those cases where you're changing the syntax to fit with the proposed change, while ignoring the actual underlying reason why the change is needed. – Flater Apr 18 '18 at 12:58
  • @PanagiotisKanavos Thanks for your comments. I will check with Type.GetTypeCode.. Actually I need to convert into switch case as per code review comments.. – Bhrathi Apr 18 '18 at 12:59
  • @DavidG there are too many assumptions in this question already. This is reflection code, possibly for deserialization. Something that works without pattern matching or even `switch`. – Panagiotis Kanavos Apr 18 '18 at 13:00