1. How can I reinterpret cast a float to an int (or a double to a long)?
float f = 2.0f;
int i = (int)f; // causes conversion
I only want to copy the bit-pattern from f
to i
. How can this be done?
2. The implicit and explicit operators in C# uses one intermediate object because the operator function is static
public static implicit operator MyClass(double s)
{
return new MyClass(s);
}
..
..
MyClass m = 2.2; // this code uses 'm' and one intermediate object.
This is fine for reference types, but for value-types which are big (say 20-30 bytes), this will cause unnecessary data copy. Is my understanding correct? And If yes, then why doesn't C# have a non-static conversion operator or user-defined assignment operator so that the conversion/assignment takes place on 'this'? If it does, whats the way to do it?