Advertisement
Qrist

Explicit Implicit Operator

Jun 30th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ImplicitAndExplicit
  4. {
  5.     public class Change
  6.     {
  7.         public uint dollar;
  8.         public ushort cent;
  9.         public Change(uint d,ushort c)
  10.         {
  11.             dollar = d;
  12.             cent = c;
  13.         }
  14.         public static explicit operator Change(float f)
  15.         {
  16.             uint dd = (uint)f;
  17.             ushort cc = (ushort)((f - dd) * 100);
  18.             return new Change(dd, cc);
  19.         }
  20.     }
  21.     class Program
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             float f = 35.75f;
  26.             Change ch = (Change)f;
  27.             Console.WriteLine(ch.dollar+ " " + ch.cent);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement