Conversion Operator Method

You design a C# conversion operator using an operation with the <<ConversionOperator>> stereotype.

You also need to declare the conversion operator using the explicit or implicit keywords. You define the conversion operator keyword by selecting the implicit or explicit value of the scope extended attribute.

In the following example, class Digit contains one explicit conversion operators and one implicit conversion operator:



public struct Digit
{
 public Digit(byte value)
  {
   if (value < 0 || value > 9) throw new ArgumentException();
   this.value = value;
  }
 public static implicit operator byte(Digit d)
 {
  return d.value;
 }
 
 public static explicit operator Digit(byte b)
  {
   return new Digit(b);
  }
 private byte value;
}