Home c# How do lambda functions work?

How do lambda functions work?




Trying to figure out how lambda functions work and saw on throw as it does the following:

delegate operation (int x, int y);
Operation Operation = (x, y) = & gt; x + y;

I understand correctly that lambda function is a short entry of the realization of the delegate or not?

And now I tried to understand how methods work in Linq, because there are also lambda expressions, for example
var s = context.users.where (p = & gt; p.id == 3) .

So I highlighted WHERE, I pressed the F12 and saw the following:

public static iQueryable & lt; tsource & gt; Where & LT; TSOURCE & GT; TSOURCE & GT; Source, Expression & LT; FUNC & LT; TSOURCE, BOOL & GT; & GT; Predicate);

This method is part of the Queryable class, but I do not understand what kind of design, why is there no body, where is registered directly by the method algorithm?

Tell me, please, how can I find similar designs or something else to understand how it works

Answer 1, Authority 100%

on the first part of the question: yes, correctly understand.

Historically, first in C # 1.1 appeared delegates and anonymous functions (as copies of delegates).

And then later in C # 3.0, lambda functions appeared as a way of brief recording anonymous functions. Well, more conveniently recorded without superfluous BoilerPlate code (Delegate of the announcement, his body is announced …)

by the second part of the question.

As far as I understand, you are interested in Extension Methods and the work of this .

maybe you may want to see the code code, I have a slaveron, which loads the sources or decompilates them, that’s what it shows by F12:

public static iQueryable & lt; tsource & gt; Where & LT; TSOURCE & GT; (
 This iQueryable & LT; TSOURCE & GT; Source
 Expression & LT; FUNC & LT; TSOURCE, BOOL & GT; & GT; Predicate)
 if (source == null)
  Throw Error.argumentNull (Nameof (Source));
 if (Predicate == NULL)
  Throw Error.argumentnull (Nameof (Predicate));
 return source.provider.createQuery & lt; TSOURCE & GT; ((Expression) Expression.call ((Expression) NULL, CachedReflectionInfo.where_tsource_2 (Typeof (TSource)), Source.Expression, Expression.Quote ((Expression) Predicate))) ;

More examples can be found there in the studio – Start learning from simple LINQ expressions and is better not enough to get enough for iQueryable, but to understand iEnumerable, for example:

source.sum (x = & gt; x.id);

under the hood looks like:

/// & lt; summary & gt; computes the sum of the sequence of & lt; see cref = "T: System. INT32 "/ & GT; Values ​​That Are Obtained by Invoking a Transform Function On Each Element of The Input Sequence. & LT; / Summary & GT;
/// & LT; Returns & GT; The Sum of the Project Values. & Lt; / Returns & GT;
/// & LT; Param name = "Source" & gt; a Sequence of Values ​​That Are Used to Calculate A SUM. & LT; / Param & GT;
/// & LT; Param name = "Selector" & gt; a transform function to apply to each element. & lt; / param & gt;
/// & lt; Typeparam name = "tsource" & gt; The Type of the Elements of & lt; paramref name = "source" /> ;< ;/typeparam>
/// & lt; Exception Cref = "T: System.argumentNullexception" & gt;
/// & lt; paramref name = "source" / & gt; OR & LT; PARAMREF NAME = "SELECTOR" / & GT; IS NULL. & LT; / Exception & GT;
/// & LT; Exception Cref = "T: System.OverflowException" & GT; The Sum IS Larger Than & LT; See Cref = "F: System.Int32.MaxValue" />
 if (source == null)
  Throw Error.argumentNull (Nameof (Source));
 if (selector == null)
  Throw Error.argumentnull (Nameof (Selector));
 int num = 0;
 Foreach (TSOURCE SOURCE1 in Source)
  Checked {Num + = Selector (Source1); }
 Return Num;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions