Someone explain this please ... (C # Lambda expression)

What's the difference between

Func<string,int> returnLength; returnLength = (string text) => { return text.Length; }; Console.WriteLine (returnLength("Hello")); 

and just

  string str = "Hello"; Console.WriteLine (str.Length); 

Are there any advantages to using one of them?

+4
source share
7 answers

This question is not entirely about when to use lambda expressions as often as when to use delegates in general. The answer is that you use delegates as a way to encapsulate behavior and convey it.

Examples of where this is useful:

  • Event handlers (the most common example is a user interface event, such as a button click)
  • Asynchronous callbacks ("run this code when you are done loading the web page")
  • LINQ operations ("filter using this predicate")
  • Starting new threads ("when the thread was created, run this code in it")

All of this can be done using interfaces; delegates are very similar to single-method interfaces. However, delegates are often a tidier solution, in part because you can create a delegate more flexibly than using an individual method or expressing a logical string with an anonymous method or lambda expression.

+2
source

The lambda gives you a function that you can now pass to other functions, rather than hard-coding access to the Length element.

+2
source

Technically, both options work. However, you can choose one for various reasons. However, for me this seems the most important:

Simplicity, readability, maintainability - some lambda expressions, especially LINQ, are shorter and more convenient to maintain than their equivalents without lambda.

In this case, it seems that the lambda is actually using more space. Thus, personally, I would prefer the second option. However, in my other cases, lambdas are the best choice.

In terms of performance: as far as I know, lambdas are a bit slower. Of course, in this situation this difference is insignificant.

+2
source

Note that the first one can simply be rewritten as

 Func<string, int> returnLength = text => text.Length; 

The usefulness of the first is that it can be used where a delegate is needed, most often when using LINQ, since you usually provide lambda when using many extension methods that make the LINQ function.

In other situations, you can define methods that accept Func<> or Action<> parameters that encapsulate logic or behavior. The caller can indicate what exactly this logic is, all who are being addressed is to provide some logic.

+1
source

If something is missing for me, the latter looks much clearer and simpler. Why do you complicate things using lambdas?

0
source

The second option is more beneficial because it gives the same result with less code. This is more understandable and convenient. Unless you have a need for a lambda that you have not noticed, I see no reason to go with a more complex version.

0
source

The code you use using the Func method seems to be just a proof of concept or a demonstration of lambda expressions and functions; you will not use lamdas in such a simple case to find the length of a string.
Lamda expressions and functions make LOT MORE more than just line output.

The second is a fast, relatively optimized method for extracting string lengths.

There is more than one way to Rome, yes, but why go around the equator when you can just turn left here?

0
source

All Articles