In C #, which means the = = sign

There is a class called Student , which has the properties Id , Name and Phone . In the user interface form, there is a Student list as follows:

 List<Student> students=new List<Student>(); 

and finally, there is code for the dataGridview_Cellclick event, where the following code is used:

 string id = dataGridview.Rows[e.RownIndex].Cells[0].Value.ToString(); Student aStudent = students.Find(i=> i.Id== id); 

What does students.Find(i=> i.Id== id) do? What does it mean? What is the meaning of the => sign? How it works?

+7
source share
6 answers

They are called lambda expressions ... Lambda expressions use special syntax. They allow you to use functions as data, such as variables or fields. The lambda expression syntax uses the => operator. This separates the parameters and body of the anonymous function statement.

You can use this as a "Go to".

The operator => can be read as "goes", and it is always used when declaring a lambda expression. The lambda expression allows you to use a function with executable statements as a parameter, variable, or field.

See this link on MSDN for a better understanding.

+4
source

This is the goes to statement (or lambda operator ), which is used in lambda expressions (creating anonymous methods) to separate input variables from a lambda body.

In your sample students.Find(i => i.Id== id) input variable i goes into the body of the lambda i.Id == id (i.e., passed as a parameter to the anonymous method).


Also pay attention to the List<T>.Find that you use. It accepts a predicate of type T , which in your case will be Predicate<Student> . Predicated is a delegate representing a method that defines a set of criteria and determines whether a specified object matches these criteria. It has the following signature:

 public delegate bool Predicate<in Student>(Student obj) 

So, you need to pass a method that takes the student object and returns bool. You can create a normal named method for this:

 private bool IsStudentHasIdEqualTo5(Student s) { return s.Id == 5; } 

And use it like this:

 Student aStudent = students.Find(IsStudentHasIdEqualTo5); 

But you need to check different id values. There are two options: either create a field in your class that will be available inside the student predicate method, or create a class that will have both this method and the 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 ​​to validate students:

 var searcher = new StudentSearcher(id); Student aStudent = students.Find(searcher.VerfyId); 

But creating such methods and classes for each search is not very efficient. That's why we have delegates (and lambdas). Instead of declaring a new named method, you can create a method without a name (anonymous) exactly where you need it, and the compiler will generate a regular named method for you:

 Student aStudent = students.Find(delegate(Student s) { return s.Id == id; }); 

Exactly the same code can be written in lambda syntax (the delegated keyword is omitted, the parameter type is inferred, goes to , used to separate the parameter body and the method, the return keyword is also omitted):

 Student aStudent = students.Find(s => s.Id == id); 

The magic here is that the compiler will generate a class like the one shown above behind the scenes. This class will have a method with a predicate signature, and it will also have a field to capture id for the search.

+3
source

=> is the goesto operator, and this is the expression lambda expresion

See msdn

+1
source

The lambda operator separates the argument of the function from its body.

 (arg1,arg2...argn) => { //body } 

A body can also be without parentheses, but it is still a "body."

 (arg1,arg2..argn) => 1 ; Student aStudent = students.Find(i=> i.Id== id); 

Find is the Linq method that accepts a lambda expression.

It will go through each element of the students.

The element is represented by i - although student will make more sense - and is transmitted to the "body". If i.Id==id the Find method returns a student element.

+1
source
  • What does students.Find(i=> i.Id== id) do?

The situation is as follows. You have a list of Student objects and an identifier for the student you are interested in. If Student objects were saved in a collection that you defined yourself, which has a Find method that takes an identifier and returns a student with that identifier, your code will look like this:

 Student aStudent = students.Find(id); 

However, when Microsoft defined the general List collection, they could not know how it would be used - and they did not want to know. They wanted to give you the opportunity to use it with a student object or any other kind that you might think of. But that meant that they should give you a way to find the items you need by providing information about your problem that you only know. In this particular case, you know that you are looking for a Student object stored in student lists that has an Id field that matches the identifier. If I would give you an object, let us call it i , you could tell me if it is the one you are looking for by doing the following check:

 i.Id == id 

If I gave you an object called student , you could run a student test that you could tell me if it was the one you need by running the test

student.Id == id

(If you did not have an identifier, but some other information that uniquely identifies the Student object (i.e. the key), you would like to indicate another condition. Flexibility is important!)

So now we will reach the next question.

  • What does it mean?

    Let me create an agreement. You will determine what you want to name the student’s subject, and you will provide an appropriate check that selects the student you are attending. You put an identifier that will represent this student to the left of => , and a validation condition to the right. So you can have something like:

    student => student.Id == id

or if you prefer to refer to the discussed student object i instead of student , then you can write

i => i.Id == id

Take a look now. What is it? This is provided that the Student object returns a bool. This is a feature!

  • What is the meaning of the = = sign?

    This is the operator that defines the function. The argument is on the left, and the body is on the right.

  • How it works?

    Suppose you wanted me to write a function that takes a List argument and a function such as the one we are discussing and returns the appropriate student without using the Find method. I would probably write something like this.

    Student FindStudent (student list, match Func) {foreach (Student level in students) if (match (st)) return st; return st; }

    You will probably find everything familiar, except, I suppose, the type of Func you can imagine is a type of function from student to boolean. I could also use Predicate, which you can guess is a type of predicate for students.

    However, you do not need me to write this or write it myself, because Microsoft has already done this for us. The code written by Microsoft for Find() of the List<T> class is very similar to the one above.

Hope this helps!


This should cover everything you requested. But for a broader picture, you can also take a look at:


A few notes that may help you:

  • Lambda expressions can have several arguments, for example. `(x, y) => x * x + y + 43 '
  • The right side of lambda expressions can be a block of code, for example. `(x, y) => {return x * x + y + 43;} '
  • The type of lambda expression with string and the int parameter that returns the value bool is `Func '
  • The type of lambda expression with a string argument and an int argument that returns nothing is the "Action"
+1
source

List<T> defines the Find(Predicate<T> match) method.

Predicate<T> is a delegate that can reference any method that matches its signature

 bool Predicate(T param) 

In your case, you call the Find method on the list of students List<Student> , so the Find method expects a function that matches the following signature:

 bool MyFindMethod(Student param) 

You can define such a method in your class as follows:

 bool MyFindMethod(Student param) { // Logic goes here } 

and pass it to your Find method as follows:

 students.Find(MyFindMethod) 

The method you use is small and simple, so the overhead of creating a method in your class is not worth the lambda expressions allowing you to very accurately define the same method.

 s => s.Id == id 

equivalent to:

 bool AnonymousMethod(Student param) { return s.Id == id; } 

The element on the left side of the operator => is the parameters that are passed to the method, and the elements on the right side of the operator => are the body of the method.

Note that the compiler is smart enough to understand that the parameter ( s in my example) is of type Student , so this need not be specified.

If you have a list of another type of EG

 public class Customer { public string Name { get; set;} } public IList<Customer> customers = new List<Customer>(); 

then the compiler will infer that the parameter is of type Customer , not a student.

 customers.Find(c => c.Name == name); 

Note that the parameter can be named whatever you want, but it is usually stored in one letter so that the expression is concise.

If you understand all this, you will see that your code

 students.Find(i => i.Id == id) 

basically calls a method that takes Student as a parameter and evaluates it to see if it matches the criteria on the right side of the => operator. If the parameter matches the criteria (that is, if the students Id match the variable Id ), the expression will return true. This tells Find that it found a match, and this object will be returned.

I answered a similar question here that is related to WPF, but an example in a different context may help you understand.

0
source

All Articles