C # generic methods and generic Interfaces

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace generic
  6. {
  7. Class generic interface
  8. {
  9. Public static void main ()
  10. {
  11. Personmanager man = new personmanager ();
  12. Person per = new person ();
  13. Man. printyourname (PER );
  14. Person p1 = new person ();
  15. P1.name = "p1 ";
  16. Person P2 = new person ();
  17. P2.name = "p2 ";
  18. Man. swapperson <person> (ref P1, ref P2 );
  19. Console. writeline ("P1 is {0}, P2 is {1}", p1.name, p2.name );
  20. Console. Readline ();
  21. }
  22. }
  23. // Generic interface
  24. Interface iperson <t>
  25. {
  26. Void printyourname (t );
  27. }
  28. Class person
  29. {
  30. Public string name = "Aladdin ";
  31. }
  32. Class personmanager: iperson <person>
  33. {
  34. # Region iperson <person> Member
  35. Public void printyourname (person t)
  36. {
  37. Console. writeline ("My name is {0 }! ", T. Name );
  38. }
  39. # Endregion
  40. // Exchange two people. Haha .. This world ..
  41. // The generic method t applies to parameters and methods.
  42. Public void swapperson <t> (ref t P1, ref t P2)
  43. {
  44. T temp = default (t );
  45. Temp = p1;
  46. P1 = P2;
  47. P2 = temp;
  48. }
  49. }
  50. }

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.