I have had recent开发者_如何学JAVAly two telephone interviews.
In both interviews I have been asked as the last question to define a Lambda expression.
I claimed that Lambda expression is an unnamed method in place of a delegate. But somehow that wasn't enough.
I find it very hard to explain this precisely on a telephone interview.
Does anyone know better?
Lambda Expressions are nameless functions given as constant values. They can appear anywhere that any other constant may, but are typically written as a parameter to some other function. The canonical example is that you'll pass a comparison function to a generic "sort" routine, and instead of going to the trouble of defining a whole function (and incurring the lexical discontinuity and namespace pollution) to describe this comparison, you can just pass a lambda expression describing the comparison.
HOWEVER, this misses one of the most important features of Lambda Expressions, which is that they execute in the context of their appearance. Therefore, they can use the values of the variables that are defined in that context. This differentiates function-pointers from true lambda expressions. In languages supporting mutable variables, proper lambda expressions offer the power to change the values of those variables.
Lambda expressions appear (with different syntax) in all LISPs, Perl, Python, and sufficiently-recent versions of C++, Objective C, C# and Java 8, but notably not in C even though it has a way to deal with passing functions (or some excuse for them) around as parameters. They are a syntax element with particular semantics, and those semantics place more requirements on the runtime than C was designed to require.
A lambda expression is a nameless suspension of code.
Consider this anonymous "multiply two things" function (a.k.a. lambda expression), using some very non-specific notation.
λ(x, y) -> x * y
Your answer was very specific to a place where you've used lambdas (I'm guessing C#?), and I suspect the interviewer was asking for a more general understanding. The concept of a delegate, the language C#, and the idea of a method are all secondary to what a lambda is and how they work. You can compute using lambda expressions on paper, for instance, with no methods involved.
To answer your revised question, for that, you answer is actually good. The only change I'd make is to stress the word "inline".
Lambda expression is an unnamed method that is written inline in place a where a delegate is needed.
Maybe they just wanted to hear that LINQ was "Language-Integrated Query".
That being said, if they really want an explanation of "what" LINQ is comprised of, I would have probably included more information that you provided. Something like:
LINQ, or Language-Integrated Query, is a set of language additions and framework classes added in .NET 3.5 which enable a more functional approach to query operations. It's based upon extension methods for IEnumerable and IQueryable and their generic counterparts which allow deferred execution in LINQ to Objects and remote processing via IQueryable, as well as many other features. There were also language changes made to C# and VB.NET to support a more "natural" query syntax directly in the language.
Well I said that Linq is an unnamed method in place of a delegate.
Actually, that's not LINQ at all, but a "lambda expression". And technically, LINQ doesn't even use them.
LINQ stands for "Language Integrated Query". Quite specifically, it's the "from...where.. select" keywords (i.e., the query syntax that is integrated into the language).
Now, to get those keyword to do things, a lot more was added to the language (and the CLR) (such as lambdas, extension methods, the Enumerable class etc).
You should look in Lambda Expression in msdn
They're probably looking for you to know that LINQ is the new DSL for querying IQueryable
and IEnumerable
objects. The "from ... where ... select ..." syntax, essentially. Knowing that it's implemented under-the-covers with lambdas and functional style would probably get you bonus points.
精彩评论