A preliminary study of C # 3.0

Source: Internet
Author: User
Tags anonymous expression

C#3.0 has been introduced for some time, for various reasons, has not been to learn, these two days in the garden to see Lao Zhao's Save C # 2.0, but we really do it? It mentions some of the new features and advantages of c#3.0. It reminds me of my interest in 3.0, to study and to share with the novice.

In c#2.0, Microsoft has brought us some new features, such as generics, anonymous delegates, and so on. However, these new features give people a sense of "copying" from other languages (such as generics like C + + templates, and some features similar to something in Java). But in c#3.0, some of the new features that Microsoft has brought to me may be features that all previous development languages did not have. This undoubtedly greatly embodies the strong advantage of c#3.0 in the development of language.

Lambda expression

A 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 are => using the lambda operator. For a more detailed explanation of the lambda you can refer to MSDN. It's very clear in that.

Here's a simple example to illustrate the benefits of a lambda. Lambda provides a clearer way to implement anonymous delegation processing. For example, in 2.0. We can write code like this:

public class Example
{
   public static void Demo (System.Windows.Controls.TextBlock outputBlock)
   {
       Func<string, string> convert = delegate(string s)
          { return s.ToUpper(); };

      string name = "Dakota";
      outputBlock.Text += convert(name) + "\n";
   }
}

Use func< (The < T, tresult>) >) delegate in C # with anonymous methods.

In 3.0, we can use the lambda to more clearly pass the parameters:

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Func<string, string> convert = s => s.ToUpper ();

      string name = "Dakota";
       outputBlock.Text += convert(name) + "\n";
   }
}

The underlying type of a LAMBDA expression is one of the generic Func delegates. This allows a lambda expression to be passed as an argument without explicitly assigning it to a delegate. In particular, because many type methods in the System.Linq namespace have func< (of < (T, tresult>) >) parameters, you can pass lambda expressions to these methods without explicitly instantiating func< (of &L t; (t, tresult>) >) delegates. This will make our code more concise and logically easier to understand.

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.