/Test/keyword_implicit.cs

http://github.com/fishman/ctags · C# · 30 lines · 23 code · 4 blank · 3 comment · 2 complexity · 55abd1981d0785994063e141a2b98344 MD5 · raw file

  1. // cs_keyword_implicit.cs
  2. using System;
  3. struct Digit
  4. {
  5. byte value;
  6. public Digit(byte value)
  7. {
  8. if (value < 0 || value > 9) throw new ArgumentException();
  9. this.value = value;
  10. }
  11. // define implicit Digit-to-byte conversion operator:
  12. public static implicit operator byte(Digit d)
  13. {
  14. Console.WriteLine( "conversion occurred" );
  15. return d.value;
  16. }
  17. }
  18. class Test
  19. {
  20. public static void Main()
  21. {
  22. Digit d = new Digit(3);
  23. // implicit (no cast) conversion from Digit to byte
  24. byte b = d;
  25. }
  26. }