c# - What is the fastest way to check a type? -


instead of overloading function 100 times or creating 100 different comparers different types i've decided check type within 1 function.

for example i'm using default comparer compare values of set of types (primitives , strings) within 2 objects. contains following code:

public class defcomparer : icomparer<object> {     public int compare(object a, object b) {         .... // = a.gettype().getfield(field).getvalue(a); - not important question i'm showing a&b below different references         switch (a.gettype().name) {             case "byte":                 if ((byte)a == (byte)b) return 0;                 else if ((byte)a > (byte)b) return 1;                 else return -1;             case "uint16":                 if ((ushort)a == (ushort)b) return 0;                 else if ((ushort)a > (ushort)b) return 1;                 else return -1;             case "sbyte":                 if ((sbyte)a == (sbyte)b) return 0;                 else if ((sbyte)a > (sbyte)b) return 1;                 else return -1;             case "int16":                 ... 

here i'm using switch statement said faster chain of if/else statements. a.gettype().name returns string dynamically obtained , method involves string comparisons. doesn't sound particularly fast me. need comparer fast technically possible because it's going used on large collections of data.

q: there faster way check type of object (that not involve string comparisons)? fastest possible way?

well have in hand. use typecode

        int = 10;         type t = a.gettype();          switch (type.gettypecode(t))         {             case typecode.boolean:                 break;             case typecode.byte:                 break;             case typecode.char:                 break;             case typecode.dbnull:                 break;             case typecode.datetime:                 break;             case typecode.decimal:                 break;             case typecode.double:                 break;             case typecode.empty:                 break;             case typecode.int16:                 break;             case typecode.int32:                 break;             case typecode.int64:                 break;             case typecode.object:                 break;             case typecode.sbyte:                 break;             case typecode.single:                 break;             case typecode.string:                 break;             case typecode.uint16:                 break;             case typecode.uint32:                 break;             case typecode.uint64:                 break;             default:                 break;         } 

this supports primitives. custom objects write else if statements inside typecode.object.

i hope helps.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -