I'd rather not use Linq if I don't have to (I remove references to it when I start a new project). Lambdas look useful, though. I'm not going to get in the habit of using lambdas, however, if Linq has to tag along for the ride.
Thanks in advance
I'd rather not use Linq if I don't have to (I remove references to it when I start a new project). Lambdas look useful, though. I'm not going to get in the habit of using lambdas, however, if Linq has to tag along for the ride.
Thanks in advance
Lambdas can be used whenever you want. They're a new language feature. Linq uses them, but you don't have to use linq.
Action blah = () => { System.Console.WriteLine("Hello World!"); }
blah(); // will write Hello World! to the console
No, you can use lambdas just like anonymous methods, no LINQ is required.
Lambda expressions are completely independent of LINQ. Indeed, you can use them when targeting a .NET 2.0 project from VS2008 or VS2010. Likewise if you're using .NET 3.5, you can use expression trees without using LINQ - although that's less common in my experience.
However, I'd urge you not to avoid LINQ - it's a hugely powerful tool. I'm personally slightly wary of LINQ providers which need to do translations via Queryable
- they're harder to predict - but LINQ to Objects is pure gold. What do you dislike about it? Note that you don't have to use query expressions in order 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 were putting you off, you don't need to worry about them.
EDIT: Just to counter a comment to the question itself - you can use LINQ to Objects with .NET 2.0, via the LINQBridge project.
Lambdas can absolutely be used without linq, I do it all the time. Here's a code sample I posted elsewhere that uses a lambda with no linq in sight.
It's important to understand where lambda expressions come from and why they are used. It's true that most people hear of lambdas through LINQ, but LINQ is simply implementing a feature that was introduced in version 3.0 of the C# language as a successor (I think) to anonymous methods.
Lambdas are actually used with delegate
s and when you use a lambda, what you're actually doing is assigning a method to a delegate
. However, you can avoid using the delegate
keyword because the compiler can infer that you are using a delegate
simply because you are using a lambda expression.
Thus the following are 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 a delegate doesn't execute its methods until it is called. Thus, even though you see x => x + 1
in your lambda expression, this actually isn't executed until you call le1(someInt)
. You may have seen this in LINQ where the query isn't executed until the delegate is called.
In short (and I'm not know for my brevity), a lambda allows you to express a method call in shortform without having to declare it in the body of your class.
My source for this answer if from Daniel Solis' Illustrated C# 2008, pp.375-377.