c# what is cast class code example
Example 1: how to make a cast in c#
//If you want to make your own cast for your struct or class you
//can overload an operator. Note that you can't make a cast
//if your your target class is in inheritence with yours.
// This is if you want to manually type out (MyType)b. This is recommened
// if your cast can fail.
public static explicit operator MyType(byte[] b)
{
MyType m = new MyType();
m = MagicToConvertByteToMytype(b);
return m;
}
// This is if you want to do it automatically.
// This is recommened if your cast is defananlty safe.
public static implicit operator byte[](MyType m)
{
return m.ToBytes();
}
Example 2: how to make a cast in c#
// if we have:
float f = 12.0f;
// and we want to put the value of f into
int x;
// we can cast f to int like this:
x = (int)f //(int) is the cast