Implementation and application of c#3.0 anonymous method

Source: Internet
Author: User
Tags anonymous new features

In the previous article, you have a basic understanding of the new features of c#3.0, such as the powerful LINQ language and the application of extension methods, and today we are introducing another important new feature added to c#3.0: anonymous methods.

1. Anonymous Traceability

The history of anonymity has long been a c#2.0, and anonymous methods have been used extensively in the application scenarios of Delegates (delegate). Let me give you a few examples to briefly review:

1 when we need to invoke a callback method, we do not need to build the delegate object, just pass the callback method name, the CLR will complete the creation of the delegate object for us.

//example 1
public static void CallBackWithoutANewDelegateObject()
{
//这里QueueUserWorkItem方法需要一个委托作为参数,
//但我们仅仅传递给回调方法名,CLR可以自动为我们构造出委托对象的代码
ThreadPool.QueueUserWorkItem(SomeAsyncTask, 5);
}
private static void SomeAsyncTask(object o)
{
Console.WriteLine(o);
}

In this example we did not construct any delegate object, but only passed the callback method name, and the CLR helped me build the code to create the delegate object.

2 We do not even have to explicitly define the callback method, we only need to use the delegate keyword inline write method code to execute the call.

//example 2
public static void CallbackWithoutNewingADelegateObject()
{
//我们在这儿以内联形式写出回调方法体,而没有定义任何的回调方法
ThreadPool.QueueUserWorkItem(
delegate(object o) { Console.WriteLine(o); },
5);
}

In this example we do not define the method Someasynctask above, but instead write the method body inline.

3 We can not even write out the arguments to invoke the method, and the CLR will generate the correct delegate object for us.

//example 3
this.BT_LOGIN.Click +=
delegate { MessageBox.Show("Button Login has been clicked!"); };

This is the most common use scenario for anonymous methods, which is to write the method code directly using delegate when adding an event handler function for a control without having to define a method function separately. When our event handler is short (like the code above, we just pop up a dialog box to display a message.) We can use this method and it would be cumbersome to define a function differently. And the parameters of the method can also be omitted (e.g. object sender here, EventArgs e)

2. Anonymous methods in c#3.0

1 implicit type variable (implicitly typed local variables)

var var1 = 1;
var var2 = 2;
var var3 = var1 + var2;
var var4 = "I'm a string.";

Here we can see that we do not specify the type of the variable (int, string, ...), but the compiler will help us accomplish this. Friends who are familiar with scripting languages may be pleasantly surprised by this syntax, but note that C # is still a strongly typed language and all types are determined at compile time, rather than waiting to run like a script, as the following illustration clearly illustrates:

Related Article

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.