C #=> anonymous method

Source: Internet
Author: User

1. Anonymous Method

C # provides a mechanism for delegation. You can define anonymous methods for the delegation. If there is no name for an anonymous method, the compiler will specify a name.

In an anonymous method, you cannot use a jump statement to jump to the external side of the anonymous method or to the internal side of the method.

The ref and out parameters cannot be used outside the anonymous method.

 

Public Class A {} // B inherits from a public class B: A {} class program {// The return type of the delegate is a public delegate a mydelegate (); static void main (string [] ARGs) {// Add method mydelegate my = Method to the delegate; // The return type of the method can be derived from the return type defined by the delegate, this is my (); // ※if the return type of the delegate is exchanged with the return type of the method, A compilation error will occur} // The method returns the type of a subclass B, and B inherits from a static B method () {return New B ();}}

 

 

2. The [Lambda (lambda) expression] Method of the anonymous method is defined.

 

The anonymous method after C #3.0 can be defined using the lambda expression.

Lambda expressions (actually called anonymous functions) or anonymous classes can all belong to something called closures.

 

Lambda operator =>

The left side is the parameter, which is expressed in parentheses (string PARAM). It can be (PARAM) and does not define the type. The compiler will infer that only one parameter can be used without parentheses.

The right side is the implementation code, which uses curly brackets. If the Code has only one line, you can use no curly brackets or the return keyword. the compiler will add

This is a simple implementation of the lambda expression.

String str1 = "anonymous external method ";
String str2 = "inside the anonymous method ";

Mydelagate my = Param => param + str1 + str2;

Console. writeline (My ("parameter "));

 

3. Coordination and resistance of the delegated call Process

If the return type of the Delegate and the parameter passing of the delegate method cannot be correctly used, the error of "covariant and resistance" often occurs.

Extra attention

 

Return type: the return type must be covariant.

The return type of a method can be derived from the type defined by the delegate.

 

Parameter type: Variable Resistance

The parameter type passed to the delegate can be derived from the parameter type of the delegate Method

 

Return type covariant

 

Public Class A {} // B inherits from a public class B: A {} class program {// The return type of the delegate is a public delegate a mydelegate (); static void main (string [] ARGs) {// Add method mydelegate my = Method to the delegate; // The return type of the method can be derived from the return type defined by the delegate, this is my (); // ※if the return type of the delegate is exchanged with the return type of the method, A compilation error will occur} // The method returns the type of a subclass B, and B inherits from a static B method () {return New B ();}}

 

 

Variable Resistance

 

Public Class A {} // B inherits from a public class B: A {} class program {// The return type of the delegate is a public delegate void mydelegate (B ); static void main (string [] ARGs) {// Add method mydelegate my = Method to the delegate; // The type of parameters passed to the delegate can be derived from the parameter type of the delegate method, this is the anti-change my (New B (); // ※if the type of the delegate parameter is exchanged with the type of the parameter of the method, A compilation error occurs.} // The method returns the type of a subclass B, and B inherits from a static void method (a ){}}

 

 

// An instance of type B inherited from a can be converted to a, while a compilation error occurs by default when the child class is converted to the parent class.
// The following code
A aa = new ();
B BB = new B ();
// This sentence is correct.
AA = BB;
// There is a problem when the subclass is converted to the parent class
BB = AA;

 

Both the above changes are caused by this reason.

 

By: http://blog.csdn.net/gishero/article/details/5161826

C #=> anonymous method

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.