Answers for "how to make a cast in c#"

C#
0

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();
}
Posted by: Guest on March-25-2021
0

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
Posted by: Guest on March-25-2021

C# Answers by Framework

Browse Popular Code Answers by Language