C # Abstract modifier Analysis

Source: Internet
Author: User

C # language is worth learning a lot. Here we mainly introduce the C # Abstract modifier, including the introduction that a member must be implemented in a forced inheritance class. .

C # What does abstract modifier mean?

C # Abstract modifiers can be used for classes, methods, attributes, events, and index indicators (Indexer ), abstract members cannot be declared as abstract members together with static and virtual abstract members, but as long as there are still unimplemented abstract members (that is, abstract classes) in the class ), then its object cannot be instantiated. It is usually used to force the inheritance class to implement a certain member.

Example:

 
 
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace example04
  5. {
  6. # Region base class, abstract class
  7. Public abstract class baseclass
  8. {
  9. // Abstract attribute. The get and set accessors indicate that the inherited class must implement the attribute as read/write.
  10. Public abstract string attribute
  11. {
  12. Get;
  13. Set;
  14. }
  15. // Abstract method. Input a string parameter without returning a value
  16. Public abstract void function (string value );
  17. // Abstract event. The type is the predefined system proxy (delegate): eventhandler.
  18. Public abstract event eventhandler event;
  19. // Abstract index indicator. Only get accessors indicate that the index indicator must be read-only for the inherited class.
  20. Public abstract char this [int Index]
  21. {
  22. Get;
  23. }
  24. }
  25. # Endregion
  26. # Region inheritance class
  27. Public class deriveclass: baseclass
  28. {
  29. Private string attribute;
  30. Public override string attribute
  31. {
  32. Get
  33. {
  34. Return attribute;
  35. }
  36. Set
  37. {
  38. Attribute = value;
  39. }
  40. }
  41. Public override void function (string value)
  42. {
  43. Attribute = value;
  44. If (event! = NULL)
  45. {
  46. Event (this, new eventargs ());
  47. }
  48. }
  49. Public override event eventhandler event;
  50. Public override char this [int Index]
  51. {
  52. Get
  53. {
  54. Return attribute [Index];
  55. }
  56. }
  57. }
  58. # Endregion
  59. Class Program
  60. {
  61. Static void onfunction (Object sender, eventargs E)
  62. {
  63. For (INT I = 0; I<(Deriveclass) sender). Attribute. length; I ++)
  64. {
  65. Console. writeline (deriveclass) sender) [I]);
  66. }
  67. }
  68. Static void main (string [] ARGs)
  69. {
  70. Deriveclass tmpobj = new deriveclass ();
  71. Tmpobj. Attribute = "1234567 ";
  72. Console. writeline (tmpobj. Attribute );
  73. // Associate the static function onfunction with the event of the tmpobj object
  74. Tmpobj. event + = new eventhandler (onfunction );
  75. Tmpobj. function ("7654321 ");
  76. Console. Readline ();
  77. }
  78. }
  79. }

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.