C#==> anonymous method

Source: Internet
Author: User

http://blog.csdn.net/gishero/article/details/5161826 a little something to look at.

1, anonymous method

C # provides a mechanism for delegates to define anonymous methods for delegates, anonymous methods do not have names, and the compiler will specify a name

An anonymous method cannot use a jump statement to jump outside of the anonymous method or jump to the inside of the method.

Ref and out parameters that cannot be used outside of anonymous methods

[C-sharp]View Plaincopy
  1. Defining delegates with anonymous methods
  2. Class Program
  3. {
  4. delegate string mydelagate (string val);
  5. static void Main (string[] args)
  6. {
  7. string str1 = "anonymous method external";
  8. //The brackets section defines a method that does not have a name, and the compiler will specify a name
  9. Mydelagate my = delegate (string param)
  10. {
  11. string str2 = "anonymous method inside";
  12. return param + str1 + str2;
  13. };
  14. //anonymous method for invoking delegates
  15. Console.WriteLine (My ("parameter"));
  16. //From the results you can see that the anonymous method also achieves the effect of defining the method for the delegate
  17. Console.read ();
  18. }
  19. }

2, the "lambda (lambda) expression" method for anonymous methods is defined

c#3.0 after the anonymous method can be defined by using the lambda expression

Either the lambda (lambda) expression (which is actually called an anonymous function) or an anonymous class can be attributed to something called a closure.

Lambda operator = =

The left is a parameter, expressed in parentheses (string param), can be (param) so does not define the type, the compiler will infer that only one argument can not use parentheses

On the right is the implementation code, using curly braces, if the code is only one line, then not using curly braces and the return keyword can also, the compiler will add to us

This is a simple implementation of the lambda expression

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

Mydelagate my = param + param + str1 + str2;

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

3, covariant and anti-change of the delegate invocation process

With regard to the return type of the delegate and the parameter passing of the delegate method, "covariance and anti-change" errors often occur if we do not use them correctly.

Additional attention required

Return type: The return type needs to be aware of the covariant

The return type of the method can be derived from the type of the delegate definition.

Parameter type: The parameter type needs to be noted is the resistance to change

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

Covariance of return types

[C-sharp]View Plaincopy
  1. Public class A {}
  2. b inherits from a
  3. Public class B:a {}
  4. Class Program
  5. {
  6. //The return type of the delegate is a
  7. public Delegate A MyDelegate ();
  8. static void Main (string[] args)
  9. {
  10. //Add method to delegate
  11. MyDelegate my = Method;
  12. The return type of the method can be derived from the return type of the delegate definition, which is the covariant
  13. My ();
  14. //※, if the return type of the delegate is interchanged with the return type of the method methods, a compilation error is generated
  15. }
  16. The//method returns the type of subclass B, and b inherits from a
  17. static B Method ()
  18. {
  19. return new B ();
  20. }
  21. }

Parameter type of the resistance variable

[C-sharp]View Plaincopy
  1. Public class A {}
  2. b inherits from a
  3. Public class B:a {}
  4. Class Program
  5. {
  6. //The return type of the delegate is a
  7. public delegate void MyDelegate (b b);
  8. static void Main (string[] args)
  9. {
  10. //Add method to delegate
  11. MyDelegate my = Method;
  12. The type of the parameter passed to the delegate can be derived from the parameter type of the delegate method, which is the resistance variable
  13. My (new B ());
  14. //※, if the type of the parameter of the delegate is interchanged with the type of the method's parameter, a compilation error is generated
  15. }
  16. The//method returns the type of subclass B, and b inherits from a
  17. static void Method (a a) {}
  18. }

In fact, an instance of type B that inherits from a can be converted to a, and the child class is converted to the parent class, and by default there is a compilation error
The following code
A aa = new A ();
b bb = new B ();
That's not a problem.
AA = BB;
A problem exists in the process of transforming a subclass into a parent class
bb = AA;

The above covariant and anti-change are caused by this cause

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.