Simplest Data type cast operator : Ternary Operator « Operator « C# / CSharp Tutorial






using System;

class MyDataType
{

   public static explicit operator int(MyDataType li)   // Convert type
   {
      Console.WriteLine("explicit operator int");
      return 0;
   }

   public static explicit operator MyDataType(int x)    // Convert type
   {
      Console.WriteLine("public static explicit operator MyDataType");
      return new MyDataType();
   }

}

class MainClass
{
   static void Main()
   {
      MyDataType d = (MyDataType)5;
      int Five = (int)d;

      Console.WriteLine(Five);
   }
}
public static explicit operator MyDataType
explicit operator int
0








3.8.Ternary Operator
3.8.1.The ? Operator
3.8.2.The ternary operator
3.8.3.Use ternary operator in Console.WriteLine function
3.8.4.Prevent a division by zero using the ? operator
3.8.5.Simplest Data type cast operator