New, virtual, override, interface, Delegate, event ----- C # -- method implementation Summary

Source: Internet
Author: User
C # -- method implementation Summary

This article is written in two parts. The first part is the understanding and problem of C # method implementation, and the second part is the type of method implementation in C.

Part 1: Understanding and problems of C # method implementation

Understanding:

1. Use the virtual and override keywords to sign the same method in the base class and the derived class to implement different methods.

2. Use the interface to sign different methods in different classes using the same method

3. Use delegate to aggregate methods in different classes and call them together to complete a comprehensive function.

4. Use events to complete the same functions as delegate

All the above four items indicate the object-oriented polymorphism.

Problem:

How to differentiate between delegation and events?

I confused them, but I cannot tell them clearly.

Part 2: Implementation of methods in C # is divided into the following types:

1. Basic Method Declaration

ReturntypeMethodname(Parameterlist)

{

// Method subject

}

2. Declare the New Method

If the base class and the derived class declare two methods with the same signature, a warning message will appear during compilation (the method of the derived class will shield the methods in the base class)

If the new keyword is used in a derived class to declare a method with the same signature as the base class, no warning message is displayed during compilation. The method of the derived class still shields the methods in the base class, only warning information is not displayed during compilation.

Example:

Class horse: mammal

{

New public void talk ()

{

// Method subject

}

}

3. Declare the virtual Method

Purpose of declaring Virtual Methods: provide different implementations of the same method, and these methods are interrelated because they are designed to accomplish the same task, different classes (different classes can only be derived classes) have different implementations.

Declare the method as virtual using the virtual keyword

Example:

Namespace System

{

Class Object

{

Public Virtual string tostring ()

{

// Method master

}

4. Declare the override method

If the base class declares a method as a virtual method, the derived class can use the override keyword to declare another Implementation of the method.

Example:

Class horse: mammal

{

Public override string tostring ()

{

}

}

A new implementation of a method in a derived class can call the original implementation of the method in the base class, which should be implemented using the base keyword.

Example:

Public override string tostring ()

{

Base. tostring ();

}

Note:

Use the virtual and override keywords to declare the rules that follow the polymorphism method:

A. A private method cannot be declared using virtual and override; otherwise, a compilation error occurs.

B. the signatures of the two methods must be completely consistent, and the return types must be the same. That is to say, they must have the same name, the same parameter type, the same number of parameters, and the same return type.

C. The two methods must have the same accessibility. For example, if one of the methods is public, the other must be public (the method can also be protect)

D. Only the override method can be used to override the virtual method. If the base class method is not virtual, if you try to rewrite it with override, a compilation error will occur.

E. if the derived class does not use override to declare the method, it will not override the base class method. It will become another Implementation of the method that is completely irrelevant to the base class method. This method just happens to have the same name as the base class method, this will cause a warning during compilation: This method will hide the inherited methods with the same name. You can use the new keyword to eliminate this warning.

F. an override method is implicitly a virtual method, which can be override in future Derived classes and cannot be explicitly declared by virtual.

5. Extension Method

An extension method is defined in a static class. the type to be extended must be the first parameter of the method and an this keyword must be appended.

The extension method allows you to extend existing types (regardless of the class or structure) by attaching static methods. Any statement can immediately use these static methods once it references the data of the extended type.

Example:

Static class util

{

Public static int negate (this int I)

{

Return-I;

}

}

To use the extension method, you only need to let the util class enter the scope (if necessary, add the using statement to specify the namespace where the util class is located), and then you can simply use ". to reference methods.

Example:

Int x = 591;

Console. writeline ("X. Negate {0}", util. Negate (x ));

6. Interface

The interface name starts with an uppercase letter I. The same method name has different implementation methods in different classes (different classes do not refer to the derived classes.

The interface only contains the method name, response type, and parameters. The specific implementation interface of the method is not concerned.

(My understanding: the interface is just a declaration method, not a implementation method)

You cannot specify any access modifiers (public, private, or protected cannot be restricted) in the interface)

Example:

Interface icomparable

{

Int compareto (Object OBJ)

}

To implement an interface, you need to declare a class or structure so that they can inherit from the interface and implement all methods specified by the interface.

Note:

To implement an interface, you must ensure that each method fully matches the method in the interface corresponding to it.

Method A and return type exactly match

B. All parameters are exactly matched (including the ref and out keyword modifiers)

C uses the interface name as the method name prefix, which is called the explicit interface implementation. We should usually use the explicit interface implementation

D. All methods used to implement an interface must have public accessibility. If an explicit interface is used for implementation, the access modifier should not be added to the method.

7. Delegate (delegate)

A delegate is a pointer to a method. You can call methods in different classes by specifying a delegate name.

Delegate usage:

A delegate declares in the class,

B. Create a delegated instance,

C. Add other classes to the delegate instance in the class constructor.

D. Compile a method call delegate instance in the class

Note:

The delegate declared in the namespace does not need to add a method in the constructor. You do not need to write a method call delegate in the class. You can directly call the delegate.

When using delegation, there are two methods with parameters:

A: Create a method adapter to implement Delegation

B: Use lambda expressions to implement Delegation

8. Events

The event is declared on the basis of the delegate.

Specific Practices:

A: First declare the delegate in the class;

B: Use the declared delegate to declare the event

C: Subscribe to events (add methods for events)

D: trigger an event (similar to a delegate, using an event as a method call)

Note:

An event can only be triggered by a class that defines it. triggering an event outside the class will cause a compilation error.

Lambda expressions also apply to events

Related Article

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.