Custom type conversion in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. // Custom Conversion
  5. // All user-defined conversions are static and use the static keyword.
  6. // User-Defined display and implicit display of conversions. They are declared using the implicit (implicit conversion) or explicit it (display conversion) keywords.
  7. // Static access rhetorical device conversion rhetorical device operator Conversion Type (parameter)
  8. Namespace countstrdemo
  9. {
  10. Class custom type conversion
  11. {
  12. // Public static void main (string [] ARGs)
  13. //{
  14. // Mycasedemo OBJ = new mycasedemo (5, 8 );
  15. /// Implicit conversion
  16. // Float F = OBJ;
  17. // Console. writeline (F. tostring ());
  18. //// Explicit conversion
  19. // Int I = (INT) OBJ;
  20. // Console. writeline (I. tostring ());
  21. // Console. Read ();
  22. //}
  23. Class mycasedemo
  24. {
  25. Public int X;
  26. Public int y;
  27. Public mycasedemo (int x, int y)
  28. {
  29. This. x = X;
  30. This. Y = y;
  31. }
  32. // Difference from the Operation overload. Operator follows the type to be converted to, not a symbol. It is also the return type of this method.
  33. // It must also be declared using the implicit (implicit conversion) or explicit it (display conversion) keyword.
  34. Public Static Implicit operator float (mycasedemo OBJ)
  35. {
  36. Return obj. y * obj. X;
  37. }
  38. Public static explicit operator int (mycasedemo OBJ)
  39. {
  40. Return obj. Y + obj. X;
  41. }
  42. }
  43. }
  44. }
    1. Using system;
    2. Using system. Collections. Generic;
    3. Using system. text;
    4. Namespace countstrdemo
    5. {
    6. Custom conversion between Class and Class
    7. {
    8. Public static void main (string [] ARGs)
    9. {
    10. C = new C ();
    11. D = C;
    12. Console. writeline (D. Name );
    13. Console. Read ();
    14. }
    15. }
    16. Class B
    17. {
    18. }
    19. Class C: B
    20. {
    21. Public string name = "C name ";
    22. Public Static Implicit Operator D (C)
    23. {
    24. D = new D ();
    25. D. Name = C. Name + "let people do it ";
    26. Return D;
    27. }
    28. }
    29. Class D: B
    30. {
    31. Public string name = "d name ";
    32. }
    33. }
    34. // Class and Class conversion before, cannot be converted to two classes that are directly or that introduce the inherited relationships,
    35. // In the above example, C-> B D-B is wrong, the only relationship is C-> D or D-C, but the conversion cannot be defined in both the CD class, otherwise, the compiler will be confused.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.