Coordination and resistance in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace CSHARP Basics
  6. {
  7. Covariant commissioned by class
  8. {
  9. // Return type covariant of the delegate Method
  10. // Define the delegate to return the person instance
  11. Public Delegate person returnmyobj ();
  12. Public static void main ()
  13. {
  14. // Register the delegate
  15. Returnmyobj MYDEL = new returnmyobj (delegated covariant. porcessmyfunc );
  16. Person reobj = MYDEL ();
  17. If (reobj is student)
  18. {
  19. Console. writeline ("the returned subclass instance ");
  20. }
  21. Else
  22. {
  23. Console. writeline ("not a subclass instance returned ");
  24. }
  25. Console. Readline ();
  26. }
  27. // According to the delegate signature, but the returned is a subclass instance
  28. Public static person porcessmyfunc ()
  29. {
  30. Return new student ();
  31. }
  32. }
  33. Class person
  34. {
  35. Public string name;
  36. Public int age;
  37. }
  38. Class student: person
  39. {
  40. Public int stuid;
  41. }
  42. }
  43. // Resistance to changes: Changes to the parent class
  44. // Covariant: Changes to the subclass direction

 

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.