Microsoft developers are very lazy, so they add many things to C # .NET, making C # .NET developers lazy.
1. Review the concept of Delegate
Please read the previous article to recall the concepts of delegates here: Callback in C # — Delegate, Action, Predicate, Func
As mentioned in the previous post, a delegate is a data type to point to a function, so when assigning a value to a delegate, we have to assign a function, as in the following example:
public delegate void GiveGiftDelegate(string gift);
public void GiveGift (string gift) {
Console.Write (“Gave” + gift);
}
//When using:
GiveGiftDelegate dlg = GiveGift;
// Pass the function in, not execute the function so there is no sign ()
2. Anonymous function
So, every time we want to pass a function to a delegate, we have to define that function; quite annoying. In javascript has an anonymous method, write the function straight without define as follows:
// Use anonmyous function, GiveGiftDlg is now a delegate
var GiveGiftDlg = function(gift) {alert ("gift"); };
function isHome(vo, GiveGift) {
var gift = "Gift received";
GiveGift(gift);
}
// Use in code
isHome (vo, GiveGiftDlg);
Fortunately, in C #, we can also write an anonymous function in the following way (From .NET 2.0):
public delegate void GiveGiftDelegate(string gift);
GiveGiftDelegate dlg =
delegate(string gift){ Console.WriteLine("Give " + gift); };
We find writing delegate this way is quite cumbersome, right? Microsoft saw the same thing, and they added lambda expressions and .NET 3.0. Want to know what a lambda expression is, see below.
3. Lambda expression
I want to repeat the nth thing: “The old Microsoft developers are very lazy, so they add many things to C # .NET, make C # .NET developers lazy.” We can understand a lambda expression is a more concise way to write an anonymous function:
// Old way
GiveGiftDelegate dlg = delegate (string gift) {Console.WriteLine ("Give " + qua); };
// Use a lambda expression
GiveGiftDelegate lamdaDlg = (gift) => {Console.WriteLine ("Gift:" + gift); }
// The complete statement of lambda expression.
// The "=>" sign is called go-to
(parameters) => {statement}
Here are some rules for writing lambda expressions:
//1. It is possible to ignore the data type of the passed parameter
(string gift) => {Console.WriteLine ("Give gift:" + gift);}
(gift) => {Console.WriteLine ("Gift:" + gift);}
//2. If there are no parameters, leave the () blank
() => {Console.WriteLine ("Hello");}
// 3. If there is only 1 parameter, you can remove the () sign.
(x) => {Console.WriteLine ("Hello" + x);}
x => {Console.WriteLine ("Hello" + x);}
// 4. If there are many parameters, separate them with commas
(x, y) => {Console.WriteLine ("Hello" + x + y);}
// 5. If the anonymous function has only 1 statement, you can remove the {}
x => {Console.WriteLine ("Hello" + x); }
x => Console.WriteLine ("Hello" + x)
// 6. If only return 1 value, you can omit the word return.
// The following 4 lambda expressions are equivalent
(x) => {return x> 4; }
x => {return x> 4; }
x => return x> 4
x => x> 4
At this point, some of you will be surprised: “Wow, so it is.” This is how we apply the above steps to reduce a lambda expression.
4. Lambda Expression and LINQ
Have you ever seen what param passed to a function of LINQ such as Where First ?. Yes, it is a delegate for a function that returns bool (See more about Func in the previous post).
Therefore, we use a lambda expression to pass an anonymous function to the Where or First function. For example, when you use LINQ’s Where function to find elements in an array:
var studentList = new List <Student> ();
// This neat little thing is a lambda expression
var students = studentList.Where (stu => stu.Age> 20);
// If we don't have it, we have to write something that is both wordy and disgusting as follows
var student = studentList.Where (new delegate (Student stu) {return stu.Age> 20;});
// Or worse
public bool FindStudentWithAge (Student stu) {return stu.Age> 20; }
var student = studentList.Where (FindStudentWithAge);
Congratulations on learning one more interesting thing that few people know in C # .NET. Lambda expressions and anonymous functions are specialized issues in .NET; you may be asked if you want to apply for a senior developer position.
The article ends here.