0

I'm working on a C# project, and I found myself with this situation:

TypeCode code = value == null ? TypeCode.DBNull : TypeCode.GetTypeCode(value.GetType());

switch(code) {
    case TypeCode.DBNull: data = SerializeNull(); break;
    case TypeCode.String: data = SerializeString((String) value); break;
    case TypeCode.Int32: data = SerializeInt32((Int32) value); break;
    // ... MORE TYPES
}

My question is:

Is there any advantage of using this "type determination" technique over this one?:

if(value == null) data = SerializeNull();
else if(value is String) data = SerializeString((String) value);
else if(value is Int32) data = SerializeInt32((Int32) value);

As for my opinion, I find the last one more readable and easy to implement.

I still wanted to know if maybe there's a performance issue

Thanks in advance

Matias Cicero
  • 25,439
  • 13
  • 82
  • 154

0 Answers0