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
- Defining delegates with anonymous methods
- Class Program
- {
- delegate string mydelagate (string val);
- static void Main (string[] args)
- {
- string str1 = "anonymous method external";
- //The brackets section defines a method that does not have a name, and the compiler will specify a name
- Mydelagate my = delegate (string param)
- {
- string str2 = "anonymous method inside";
- return param + str1 + str2;
- };
- //anonymous method for invoking delegates
- Console.WriteLine (My ("parameter"));
- //From the results you can see that the anonymous method also achieves the effect of defining the method for the delegate
- Console.read ();
- }
- }
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
- 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 to delegate
- MyDelegate my = Method;
- The return type of the method can be derived from the return type of the delegate definition, which is the covariant
- My ();
- //※, if the return type of the delegate is interchanged with the return type of the method methods, a compilation error is generated
- }
- The//method returns the type of subclass B, and b inherits from a
- static B Method ()
- {
- return new B ();
- }
- }
Parameter type of the resistance variable
[C-sharp]View Plaincopy
- 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 b);
- static void Main (string[] args)
- {
- //Add method to delegate
- MyDelegate my = Method;
- 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
- My (new B ());
- //※, if the type of the parameter of the delegate is interchanged with the type of the method's parameter, a compilation error is generated
- }
- The//method returns the type of subclass B, and b inherits from a
- static void Method (a a) {}
- }
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