C# Lambda expressions: Why should I use them?

后端 未结 15 1192
栀梦
栀梦 2020-11-22 03:51

I have quickly read over the Microsoft Lambda Expression documentation.

This kind of example has helped me to understand better, though:

delegate in         


        
相关标签:
15条回答
  • 2020-11-22 04:09

    A lot of the times, you are only using the functionality in one place, so making a method just clutters up the class.

    0 讨论(0)
  • 2020-11-22 04:11

    The innovation is in the type safety and transparency. Although you don't declare types of lambda expressions, they are inferred, and can be used by code search, static analysis, refactoring tools, and runtime reflection.

    For example, before you might have used SQL and could get an SQL injection attack, because a hacker passed a string where a number was normally expected. Now you would use a LINQ lambda expression, which is protected from that.

    Building a LINQ API on pure delegates is not possible, because it requires combining expression trees together before evaluating them.

    In 2016 most of the popular languages have lambda expression support, and C# was one of the pioneers in this evolution among the mainstream imperative languages.

    0 讨论(0)
  • 2020-11-22 04:11

    The biggest benefit of lambda expressions and anonymous functions is the fact that they allow the client (programmer) of a library/framework to inject functionality by means of code in the given library/framework ( as it is the LINQ, ASP.NET Core and many others ) in a way that the regular methods cannot. However, their strength is not obvious for a single application programmer but to the one that creates libraries that will be later used by others who will want to configure the behaviour of the library code or the one that uses libraries. So the context of effectively using a lambda expression is the usage/creation of a library/framework.

    Also since they describe one-time usage code they don't have to be members of a class where that will led to more code complexity. Imagine to have to declare a class with unclear focus every time we wanted to configure the operation of a class object.

    0 讨论(0)
  • 2020-11-22 04:13

    This is just one way of using a lambda expression. You can use a lambda expression anywhere you can use a delegate. This allows you to do things like this:

    List<string> strings = new List<string>();
    strings.Add("Good");
    strings.Add("Morning")
    strings.Add("Starshine");
    strings.Add("The");
    strings.Add("Earth");
    strings.Add("says");
    strings.Add("hello");
    
    strings.Find(s => s == "hello");
    

    This code will search the list for an entry that matches the word "hello". The other way to do this is to actually pass a delegate to the Find method, like this:

    List<string> strings = new List<string>();
    strings.Add("Good");
    strings.Add("Morning")
    strings.Add("Starshine");
    strings.Add("The");
    strings.Add("Earth");
    strings.Add("says");
    strings.Add("hello");
    
    private static bool FindHello(String s)
    {
        return s == "hello";
    }
    
    strings.Find(FindHello);
    

    EDIT:

    In C# 2.0, this could be done using the anonymous delegate syntax:

      strings.Find(delegate(String s) { return s == "hello"; });
    

    Lambda's significantly cleaned up that syntax.

    0 讨论(0)
  • 2020-11-22 04:14

    It's a way of taking small operation and putting it very close to where it is used (not unlike declaring a variable close to its use point). This is supposed to make your code more readable. By anonymizing the expression, you're also making it a lot harder for someone to break your client code if it the function is used somewhere else and modified to "enhance" it.

    Similarly, why do you need to use foreach? You can do everything in foreach with a plain for loop or just using IEnumerable directly. Answer: you don't need it but it makes your code more readable.

    0 讨论(0)
  • 2020-11-22 04:16

    Lambda's cleaned up C# 2.0's anonymous delegate syntax...for example

    Strings.Find(s => s == "hello");
    

    Was done in C# 2.0 like this:

    Strings.Find(delegate(String s) { return s == "hello"; });
    

    Functionally, they do the exact same thing, its just a much more concise syntax.

    0 讨论(0)
提交回复
热议问题