Operator Overloading in C #

Source: Internet
Author: User
    1. Using system;
    2. Using system. Collections. Generic;
    3. Using system. text;
    4. Namespace countstrdemo
    5. {
    6. Class comparison operator overload
    7. {
    8. Public static void main (string [] ARGs)
    9. {
    10. // There are 6 Comparison operators and 3 pairs:
    11. // =! =
    12. //> <
    13. // >=< =
    14. // Duplicate load. For example, if the value = is reloaded, it must be reloaded! = The reverse return type must be bool.
    15. }
    16. Class myvectorforeq
    17. {
    18. Public int X;
    19. Public int y;
    20. Public myvectorforeq (int x, int y)
    21. {
    22. This. x = X;
    23. This. Y = y;
    24. }
    25. // Overload = The operator must be reloaded in pairs at the same time! =
    26. Public static bool operator = (myvectorforeq lobj, myvectorforeq robj)
    27. {
    28. // Here we only compare the Member values. In ms, we recommend that you rewrite the equals method and use hash to compare the object content.
    29. If (lobj. x = robj. X & lobj. Y = robj. Y)
    30. {
    31. Return true;
    32. }
    33. Else
    34. {
    35. Return false;
    36. }
    37. }
    38. Public static bool Operator! = (Myvectorforeq lobj, myvectorforeq robj)
    39. {
    40. If (lobj. x = robj. X & lobj. Y = robj. Y)
    41. {
    42. Return false;
    43. }
    44. Else
    45. {
    46. Return true;
    47. }
    48. }
    49. }
    50. }
    51. }

    Using system;

  1. Using system. Collections. Generic;
  2. Using system. text;
  3. Namespace countstrdemo
  4. {
  5. Class operation floating overload
  6. {
  7. Public static void main (string [] ARGs)
  8. {
  9. Vector vec1 = new vector (5, 6, 7 );
  10. Vector vec2 = new vector (2, 2, 2 );
  11. Vector revec = vec1 + vec2;
  12. Console. writeline ("x = {0} y = {1} z = {2}", revec. X, revec. Y, revec. z );
  13. Console. Read ();
  14. }
  15. }
  16. Class Vector
  17. {
  18. Public int X;
  19. Public int y;
  20. Public int Z;
  21. Public vector (int x, int y, int Z)
  22. {
  23. This. x = X;
  24. This. Y = y;
  25. This. z = z;
  26. }
  27. Public vector (vector VEC)
  28. {
  29. This. x = Vec. X;
  30. This. Y = Vec. Y;
  31. This. z = Vec. Z;
  32. }
  33. // Define the overload, which is similar to the method, but must be public static... you can remember that there is no method name, hehe operator + indicates the heavy load + number operation
  34. Public static vector operator + (vector lvec, vector rvec)
  35. {
  36. Vector revec = new vector (lvec );
  37. Revec. x + = rvec. X;
  38. Revec. Y + = rvec. Y;
  39. Revec. Z + = rvec. Z;
  40. Return revec;
  41. }
  42. }
  43. }

 

Bytes -------------------------------------------------------------------------------------------------

 

 

 

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.