1 00:00:00,310 --> 00:00:04,280 Now that we've got a working console application, let's refactor it 2 00:00:04,280 --> 00:00:08,090 even more with a C# language feature called Lambda Expressions. 3 00:00:09,500 --> 00:00:13,720 We can use Lambda Expressions to represent our anonymous methods. 4 00:00:13,720 --> 00:00:17,410 Lambda Expressions can be a little daunting when you first see one, but 5 00:00:17,410 --> 00:00:20,730 as we practice they will start to feel more natural. 6 00:00:20,730 --> 00:00:23,600 We'll be using a new operator called the Lambda Operator. 7 00:00:25,250 --> 00:00:27,220 Lambdas originate from math. 8 00:00:27,220 --> 00:00:31,670 Mathematicians needed a simple way to represent a computation, 9 00:00:31,670 --> 00:00:34,290 functional programmers thought it was a great idea and 10 00:00:34,290 --> 00:00:37,570 incorporated the syntax into their languages. 11 00:00:37,570 --> 00:00:39,690 Since we're not here to learn about math, 12 00:00:39,690 --> 00:00:44,990 let's dive into how a lambda expression is written in C#. 13 00:00:44,990 --> 00:00:47,130 Lambda Expressions consist of one or 14 00:00:47,130 --> 00:00:51,310 more input parameters, a Lambda Operator and an Expression. 15 00:00:52,700 --> 00:00:55,660 So, we can write a simple Lambda like this. 16 00:00:56,835 --> 00:00:58,185 What does that mean? 17 00:00:58,185 --> 00:01:03,405 We've got an input parameter, the x in parentheses, a lambda operator, 18 00:01:03,405 --> 00:01:09,395 the equal sign followed by a greater than sign, and an expression, x + 2. 19 00:01:09,395 --> 00:01:16,335 If I were to explain this to someone in conversation, I would say x goes to x + 2. 20 00:01:16,335 --> 00:01:20,570 Whenever we see the lambda operator we can use the phrase goes to. 21 00:01:21,620 --> 00:01:24,737 So if x had a value of one at the time this function ran, 22 00:01:24,737 --> 00:01:27,250 the expression would evaluate to three. 23 00:01:29,020 --> 00:01:32,700 Let's take the functions we wrote earlier and lambdify them. 24 00:01:32,700 --> 00:01:35,150 Let's start with our conversate function. 25 00:01:35,150 --> 00:01:38,360 We don't need the delegate keyword anymore, and 26 00:01:38,360 --> 00:01:42,000 we also don't need to declare a message as a string. 27 00:01:42,000 --> 00:01:46,173 The compiler can infer what type it is by looking at the types here in our 28 00:01:46,173 --> 00:01:47,105 declaration. 29 00:01:47,105 --> 00:01:48,880 Message goes to. 30 00:01:48,880 --> 00:01:55,360 We can also turn our say greeting action down here into a lambda. 31 00:01:55,360 --> 00:01:57,690 In fact, why don't you pause the video and 32 00:01:57,690 --> 00:01:59,920 try it yourself before watching how I do it. 33 00:02:02,760 --> 00:02:04,310 Okay, here's how I would do it. 34 00:02:05,770 --> 00:02:11,790 Delete the delegate, delete string, and add a lambda operator. 35 00:02:12,870 --> 00:02:19,945 So say greeting = greeting => and then our WriteLine. 36 00:02:19,945 --> 00:02:24,197 Let's see if this works, 37 00:02:24,197 --> 00:02:30,980 mcs Program.cs and mono Program.exe. 38 00:02:30,980 --> 00:02:33,050 What you name, Carling. 39 00:02:33,050 --> 00:02:33,760 Hello, Carling. 40 00:02:33,760 --> 00:02:34,599 Nice to see you! 41 00:02:34,599 --> 00:02:36,579 You too! 42 00:02:36,579 --> 00:02:37,639 Are you doing well? 43 00:02:37,639 --> 00:02:38,739 Yep! 44 00:02:38,739 --> 00:02:39,859 Later Carling! 45 00:02:39,859 --> 00:02:41,810 Great, now we've got lambdas.