First, anonymous type
You can create an anonymous type by using the new operator and the object's initial value.
Example:
var v = new {Name = "Micro", Message = "Hello"};
var v = new[] {
New {Name = "Micro", Message = "Hello"},
New {Name = "Soft", Message = "wold!"}
};
Anonymous types are typically used in the SELECT clause of a query expression to return a subset of the properties of each object in the source sequence.
var Query = from P in v select new {p.name};
foreach (Var o in Query)
{
Console.WriteLine (O.name);
}
Can find objects
ienumerable<v> results = people. Where (Delegate (v P) {return p.name = = "Micro";});
Ii. Anonymous Methods
To pass a code block as a delegate parameter, for example:
Click events
Button1. Click + = delegate (System.Object O, System.EventArgs e)
{
System.Console.WriteLine ("Hello");
};
This. Loaded + = Delegate
{
Initializeevent ();
};
Load Events
This. Loaded + = (SL, el) =
{
System.Console.WriteLine ("Hello");
This.button1.Click + = (sender, e) = =
{
System.Console.WriteLine ("wold!");
}
}
Anonymous methods and Threads
System.Threading.Thread Thread = new System.Threading.Thread (delegate (j)
{
System.Console.WriteLine ("Hello");
});
Thread. Start ();
System.Threading.ThreadPool.QueueUserWorkItem ((s) = =
{
Action f = () =
{
System.Console.WriteLine ("wold!");
};
}
Associating a delegate with an anonymous method
delegate void DEL (string s);
private void Window_Loaded (object sender, RoutedEventArgs e)
{
DEL p = Delegate (String j)
{
System.Console.WriteLine (j);
};
P ("Hello");
}
Third, Func,func is a generic delegate with a return value
Func<int> represents no parameter and returns a delegate with a value of int
Func<object,string,int> indicates that an incoming parameter is an object, and a string returns a delegate with a value of int
Func<object,string,int> indicates that an incoming parameter is an object, and a string returns a delegate with a value of int
Func<t1,t2,,t3,int> represents a delegate with an incoming parameter of T1,T2,,T3 (generic) that returns a value of int
Func at least 0 parameters, up to 16 parameters, are returned based on the return value generic. Must have a return value, not void
Anonymous methods can only be created when using delegates, in fact, they are created by the delegate keyword.
Examples of Use:
var func = new Func<bool, int> (delegate (bool x) {return x 10:5;});
int ret = func (true);
Iv. Anonymous Events
Btn. Click + = Delegate (object o, EventArgs e) {};
v. LAMBDA expression,func<t> Delegate
MSDN writes: "Lambda expression" is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator-=
On the left is the input parameter list
1. One parameter: param=>expr
2. Multiple parameters: (param-list) =>expr
Right is an expression or statement block
(parameter list) = = Expression or statement block
A legitimate lambda expression
1, (x, y) = = x * Y//multi-parameter, implicit type = = Expression
2, x = x * 5//single-parameter, implicit type = = Expression
3, x = {return x * 5;} Single-parameter, implicit-type = statement block
4, (int x) = x * 5//single parameter, explicit type = = Expression
5, (int x) + = {return x * 5;} Single-parameter, explicit type = = Statement block
6, () = Console.WriteLine ()//No parameters
When you write a lambda expression, you can omit the type of the parameter, because the compiler is able to infer the type of the parameter directly from the context, as shown in the example code below.
(x, y) = + x + Y//multi-parameter, implicit type = = Expression
C # Anonymous delegate, anonymous method, anonymous object, lambda expression