Using custom implicit conversions in C # To overload returned values

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace countstrdemo
  5. {
  6. Class return value overload
  7. {
  8. Public static void main (string [] ARGs)
  9. {
  10. // You can see that this call is perfect.
  11. Myreturndemo OBJ = new myreturndemo ("Aladdin", 22 );
  12. Int I = obj. getinfo ();
  13. String STR = obj. getinfo ();
  14. Console. writeline (I. tostring ());
  15. Console. writeline (STR );
  16. Console. Readline ();
  17. }
  18. }
  19. Class myreturndemo
  20. {
  21. Public string name;
  22. Public int age;
  23. Public myreturndemo (string name, int age)
  24. {
  25. This. Name = Name;
  26. This. Age = age;
  27. }
  28. Private string returnbystring ()
  29. {
  30. Return "name:" + this. Name;
  31. }
  32. Private int returnbyint ()
  33. {
  34. Return this. Age;
  35. }
  36. Public mystructtemp getinfo ()
  37. {
  38. Return new mystructtemp (this );
  39. }
  40. // Structure, which is used for intermediate processing.
  41. Public struct mystructtemp
  42. {
  43. Private myreturndemo stuobj;
  44. Public mystructtemp (myreturndemo OBJ)
  45. {
  46. This. stuobj = OBJ;
  47. }
  48. Public Static Implicit operator string (mystructtemp OBJ)
  49. {
  50. Return obj. stuobj. returnbystring ();
  51. }
  52. Public Static Implicit operator int (mystructtemp OBJ)
  53. {
  54. Return obj. stuobj. returnbyint ();
  55. }
  56. }
  57. }
  58. }

 

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.