I'm in need to make a byte[] -> T extension method and need it to be fast (no need for it being pretty)This function will be called 100's of 1000's of times in very short succession in an absolute performance critical environment.We're currently optimizing on ticks level, every tick translates to a couple milliseconds higher in the callstack, thus the need of raw speed over maintainability (not how I like to design software, but the reasoning behind this is out of scope).Consider the following code, it's clean and maintainable, but it's relatively slow (probably due to boxing and unboxing), Can this be optimized to be faster?public static T ConvertTo<T>(this byte[] bytes, int offset = 0){    var type = typeof(T);    if (type == typeof(sbyte)) return bytes[offset].As<T>();    if (type == typeof(byte)) return bytes[offset].As<T>();    if (type == typeof(short)) return BitConverter.ToInt16(bytes, offset).As<T>();    if (type == typeof(ushort)) return BitConverter.ToUInt32(bytes, offset).As<T>();    if (type == typeof(int)) return BitConverter.ToInt32(bytes, offset).As<T>();    if (type == typeof(uint)) return BitConverter.ToUInt32(bytes, offset).As<T>();    if (type == typeof(long)) return BitConverter.ToInt64(bytes, offset).As<T>();    if (type == typeof(ulong)) return BitConverter.ToUInt64(bytes, offset).As<T>();    throw new NotImplementedException();}public static T As<T>(this object o){    return (T)o;}And yes it needs to be generic, sadly
Performance Byte[] to Generic
c#;performance