C # Delegate Action func predicate event anonymous function lambda

Source: Internet
Author: User

1, delegate
is a type in C # that is actually a class that holds a reference to a method.
Unlike other classes, the delegate class can have a signature (signature), and it can only hold references to methods that match its signature.
It implements functions that are very similar to the function pointers in C + +. It allows you to pass a method of Class A to the object of another class B so that the object of Class B can call this method M. However, compared to function pointers, delegate has many advantages that function pointers do not have:
First, the function pointer can only point to a static function, and delegate can reference both static and non-static member functions. When referencing a non-static member function, delegate not only holds a reference to this function's entry pointer, but also holds a reference to the class instance that called the function.
Second, delegate is an object-oriented, type-safe, reliable controlled (managed) object compared to a function pointer. In other words, runtime ensures that delegate points to an effective method, and you don't have to worry about delegate pointing to an invalid address or an out-of-bounds address.

2. Action
is a delegate that has no return value, the Action represents no parameter, no delegate that returns a value, and action<int,string> represents a delegate with an incoming parameter int,string no return value.
3. Func
A generic delegate that has a return value,func<int> represents an argument without a parameter, a delegate that returns a value of int, func<object,string,int> a delegate with anincoming parameter of object, and a string that returns a value of int.

is a generic delegate that returns a bool type.
5. Event
The delegate that modifies the delegate, without the event, is an ordinary delegate that can be called directly through the delegate, which is an event that can only be called by the member function of the class.
6. Anonymous method
There is no need to use a specific method to simplify the code,
    1. Host. Opened + = delegate (object sender, EventArgs e)
    2. {
    3. Console.WriteLine ("Service opened.");
    4. };

Anonymous methods without parameters and return values, which can be referred to by delegates with any form of signature, if there is an overloaded method, the parameter is two types of delegates, and if the method is called using an anonymous method with no parameters and a return value, the error is compiled

  1. static void Output (intdelegate id)
  2. {
  3. }
  4. static void Output (stringdelegate sd)
  5. {
  6. }
  7. static void Main (string[] args)
  8. {
  9. Output (Delegate {});
  10. }
7. Lambda expression
is also an anonymous method, (argument list) => expression or statement block, when you write a lambda expression, you can omit the type of the parameter, because the compiler can infer the type of the parameter directly from the context,

C # Delegate Action func predicate event anonymous function lambda

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.