Can lambda be used without Linq?

I would prefer not to use Linq if I don't need it (I delete links to it when starting a new project). However, Lambdas is useful. I'm not going to get used to using lambda, however, if Link needs to tag it for a ride.

Thanks in advance

+4
source share
5 answers

Lambdas can be used whenever you want. This is a new language feature. Linq uses them, but you do not need to use linq.

Action blah = () => { System.Console.WriteLine("Hello World!"); } blah(); // will write Hello World! to the console 
+23
source

Lambda expressions are completely LINQ independent. In fact, you can use them when targeting a .NET 2.0 project from VS2008 or VS2010. Similarly, if you are using .NET 3.5, you can use expression trees without using LINQ - although this is less common in my experience.

However, I urge you not to avoid LINQ - it is a very powerful tool. I personally am a little afraid of LINQ providers who need to translate through Queryable - it's harder to predict, but LINQ to Objects is pure gold. What do you dislike about this? Note that you do not need to use query expressions to use LINQ - you can write queries in "dot notation" using lambda expressions:

 var query = people.Where(p => p.Age >= 18) .Select(p => p.Name); 

... so if query expressions have disabled you, you don’t need to worry about them.

EDIT: just to meet a comment on the question itself - you can use LINQ to Objects with .NET 2.0 through the LINQBridge project.

+16
source

It is important to understand where lambda expressions come from and why they are used. It is true that most people hear about lambdas through LINQ, but LINQ just implements a feature that was introduced in version 3.0 of C # as a successor (I think) to anonymous methods.

Lambdas are actually used with delegate , and when you use lambda, what you actually do is assign a delegate method. However, you can avoid using the delegate keyword because the compiler can conclude that you are using delegate simply because you are using a lambda expression.

So the same thing:

 MyDel del = delegate (int x) {return x + 1;} ; // Using an anonymous method MyDel le1 = x => x + 1; // Using a lambda expression 

Keep in mind that the delegate does not execute its methods until it is called. That way, even if you see x => x + 1 in your lambda expression, this actually does not work until you call le1(someInt) . You may have seen this in LINQ, where the request is not executed until the delegate is called.

In short (and I don't know, for my brevity), lambda allows you to express a method call in short form without having to declare it in the body of your class.

My source for this answer is if from Daniel Solis' Illustrated C # 2008 , pp. 375-377.

+4
source

No, you can use lambdas just like anonymous methods, LINQ is not required.

+3
source

Lambdas can absolutely be used without linq, I do this all the time. Here is an example of code that I posted elsewhere that uses lambda without links in the field of view.

+2
source

All Articles