There is a class named Student
which has the properties Id
, Name
and Phone
.
In a UI form there is a list of Student
in the following way:
List<Student> students=new List<Student>();
and finally there is a code for dataGridview_Cellclick
event, where the below code is used:
string id = dataGridview.Rows[e.RownIndex].Cells[0].Value.ToString();
Student aStudent = students.Find(i=> i.Id== id);
What does the students.Find(i=> i.Id== id)
do? What does it mean? What is the meaning of =>
sign? How does it work?
They are call Lambda Expressions...Lambda expressions use special syntax. They allow functions to be used as data such as variables or fields. The lambda expression syntax uses the => operator. This separates the parameters and statement body of the anonymous function.
You can this of it as "Goes to".
The => operator can be read as "goes to" and it is always used when declaring a lambda expression. A lambda expression allows you to use a function with executable statements as a parameter, variable or field.
See this link on MSDN to understand it better.
This is a goes to
operator (or lambda operator), which used in lambda expressions (anonymous methods creation) to separate input variables from lambda body.
In your sample students.Find(i => i.Id== id)
input variable i
goes to lambda body i.Id == id
(i.e. passed as a anonymous method parameter).
Also take a look on on List<T>.Find
method, which you are using. It accepts predicate of type T
, which in your case will be Predicate<Student>
. Predicated is a delegate, which represents method that defines a set of criteria and determines whether the specified object meets those criteria. It has following signature:
public delegate bool Predicate<in Student>(Student obj)
So, you need to pass a method, which accepts a student object and returns a bool. You can create normal named method for this:
private bool IsStudentHasIdEqualTo5(Student s)
{
return s.Id == 5;
}
And use it this way:
Student aStudent = students.Find(IsStudentHasIdEqualTo5);
But you need to verify for different id values. There is two options - either create field in your class, which will be available inside student predicate method, or create class, which will have both this method and field:
class StudentSearcher
{
private int _id; // capture id
public StudentSearcher(int id)
{
_id = id;
}
// method has same signature as bool Predicate(Student obj)
public bool VerfyId(Student s)
{
return s.Id == _id;
}
}
Now you can use this named method and provide different id
values for student verification:
var searcher = new StudentSearcher(id);
Student aStudent = students.Find(searcher.VerfyId);
But creating such methods and classes for each search is not very efficient. This is why we have delegates (and lambdas). Instead of declaring new named method, you can create method without name (anonymous) exactly in place where you need it, and compiler will generate usual named method for you:
Student aStudent = students.Find(delegate(Student s) {
return s.Id == id;
});
Exactly same code could be written in lambda syntax (delegate keyword omitted, parameter type inferred, goes to
operator used to separate parameter and method body, return keyword also omitted):
Student aStudent = students.Find(s => s.Id == id);
The magic here is that compiler will generate class like one shown above behind the scene. That class will have method with predicate signature, and also it will have field for capturing id
to search for.
The lambda operator separates the function argument(s) from its body.
(arg1,arg2...argn)
=>
{
//body
}
The body could also be without parenthesis.. but it is still a "body".
(arg1,arg2..argn) => 1 ;
Student aStudent = students.Find(i=> i.Id== id);
Find is a Linq method that takes a lambda expression.
It will go through each element in students.
The element is represented by i
- although student
would make more sense - and is passed into the "body".
If i.Id==id
the Find method returns the student
element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With