Extension methods and inheritance

Source: Internet
Author: User

1. Extension Method

Requirements for extension methods:

1. The first parameter is the type to be extended or to be operated, which becomes the "extended type ".

2. to specify the extension method, add the this modifier before the extended type name.

3. Use the method as an extension method. Use Using to specify the namespace to import the extension type, or specify the extension type and call.CodeIn the same namespace.

 

Public static class directoryextension <br/>{< br/> Public static void copyto (this directoryinfo sourcedir, <br/> string target) {}< br/>}</P> <p> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> string STR = "testpath"; <br/> directoryinfo dir = new directoryinfo (STR); <br/> dir. copyto (STR); <br/>}< br/>}

 

2 base class Rewriting

1 virtual Modifier

C # supports rewriting of instance methods and attributes, but does not support rewriting of fields or any static members .. If a public or protected member does not contain a virtual modifier, the member cannot be overwritten by a subclass. The override method explicitly uses the override keyword.

2 new Modifier

It hides the re-declared member of the derived class in the face of accumulation. In this case, it is not a member that calls the derived element. On the contrary, the members of the base class search for the inheritance chain, find the member before the new modifier, and then call the member.

3 sealed modifier. In this way, the subclass is prohibited from being declared as a virtual base class member.

4 Base member.

5 abstract class. Abstract members are non-implemented methods or attributes. They force all derived classes to provide implementations. Realize polymorphism, similar to C ++

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.