Hi everyone, I am Ajay and I am a Microsoft Certified Solution Developer
Today we are gonna talk about Delegates. And we will specifically talk about Xthin Delegate with Lambda Expressions in C-Sharp
So, let's give it a go. Well, first of all let's understand what a Delegate is
Well, a Delegate is a function pointer that can point to a method that has the same signature as that of the Delegate
And then we can invoke that method using Delegate. Now many of you might be thinking that we can directly invoke a method, then why should we use a Delegate
Well, there could be some cases or scenarios or strategies in practical scenarios where we want to pass a method to another method to decide upon some kind of strategies or logic that we want to execute inside a method
So, at that point of time Delegates will really be helpful. So, as I already mentioned Delegates are used to define callback methods and they can also be used in implementing event handling
And to our surprise, they are most commonly used in link projections and syntax queries
As I already mentioned that we will specifically talk about Xthin Delegate
So, Xthin Delegate is a Delegate provided by Microsoft that can take up to 16 input parameters and can point to any method that returns WORLD
So, to understand it in more detail, we will take some more examples, we will take some practical examples
Well, to add something to it, to add more to it, well the dictionary name of Delegate is a representative or a spokesperson that we can send to another method to tell our feelings or to tell our strategies that you know what kind of logic we want to implement inside this method
So, let's take some examples of it. So, in the interest of time, I have already created a control application where I have you know a simple name method which is an entry point for our program
And I have written a simple Check Hello method that just prints a greeting message on control window and we are just calling it
So, let's make it a run. So, bear with me, it's taking a few seconds
Okay, as you can see, it prints a simple message on control window
Now, we can achieve the same thing by writing an Xthin Delegate
So, how can we write this? Xthin, as I already mentioned, it can take up to 16 parameters and it can point to a method that has the same signature as we have in our Delegate
Now, that method should return world. So, this Check Hello method accepts a string parameter and returns world
So, we can say, okay, this Xthin Delegate will accept string and it can point to a method that returns world
So, let's name this Xthin Delegate like XthinGreetingMessage. And now, this Check Hello method is a perfect delegate for our Xthin because it takes the same string parameter as in our Xthin Delegate and it also returns world
So, let's point this method to this Xthin Delegate. Now, instead of calling this method this way, we can call this method like this
Simple. So, nothing magical here. So, the same method can easily be invoked by pointing it to an Xthin Delegate
We'll see what difference does it make if we are calling method directly or calling through Delegates
Now, let's talk a little about Lambda functions. Now, instead of writing a new method altogether, we can write the same thing in a single line by using Lambda expressions
So, Lambda expressions take three things. One is input parameter. One is input parameter
Input parameter name. Input parameter name. And then arrow filter. And then the logic that we want to write
If our logic is only a single line, then we don't need to put any kind of brackets
We can directly write our single line code here. And then this single line code
Single line code. And then we can just write the same code
If we have more than one parameter to pass our Lambda expression, we need to put a bracket and we just need to write parameter name
And then we can put an arrow bracket. And for multi-column code, we need to use this scripting bracket
And just scan it on. We can write our multi-line code inside it
And the bracket type for this Lambda expression will automatically be figured out at compile time by the compiler
Let's see it in action. So, instead of writing a new method altogether, we can achieve the same thing by using Lambda expressions
So, how we can do this? Let's write another action to the script
String input parameter. And let's say greeting action with Lambda. Now, as I already said with you, first parameter indicates a single parameter
We don't need to put this kind of bracket. Input indicates a single parameter
So, we have only single parameter and name. Let's name this single parameter as x
Or maybe new x. Like we did in this case. Let's name this variable
Call variable name. And then arrow bracket. This one here. And now, what kind of operation we want to perform
We want to print that name to console.init. So, this is our single language function
This name, as we hover it, this code, print. Because compiler automatically figures it out
Now, we can call the same thing. We can use it in Lambda
Okay. And then let's invoke it. As you can see, we have invoked our first method using our traditionally and second method is using Lambda expression
Now, let's understand what's the practical use of calling a method directly or calling a method using privileges
Now, let's understand. Let's take a scenario or maybe to entertain this thought in a more detail
I have created a simple class student in which we have three properties, name, age and address
And we have a simple method. For the sake of simplicity, I have created a method showing print
And let's just assume we are putting that into database. And we are also printing or maybe showing the data into one of our control windows
To be honest, if it was a real production application, we would inject some kind of strategies by using some structure injection or maybe property injection to show data into real databases and to print data or maybe to log data into some maybe memory or maybe some kind of control application
But for the sake of simplicity, I have just written to the line here. So, assuming that first line puts data or maybe some block of line puts data into database and so on to the strategy control window
Now, let's create an instance of student class. Let's create an instance of student class
Student, let's address is USA, age is 13 and the name is Delhi
So, we have created an instance of student class and then we can just save and print on the smartphone
Nothing magical here, simple straightforward logic, right? Now, when we run this program, we see, okay, we filled data into our database and we are also printing the data on our control window, right
Now, what if after some days, a new requirement comes in that in some part of our application, we want to show our data on control window and in some part of our application, we want to log the data into some log file
So, in that case, we would either end up writing some kind of conditional logic inside this method
Okay, if this is true, then put data to control window, print to control window or A, put data to log file by maybe some concentrating or maybe by passing some kind of parameter here
Okay, or we would end up writing another method like this, save and print, save and print to control and save and print to log
And in this line, I would write like file, file.writeontext and just type this text to office in heat drive somewhere and put it, okay, this student, student log, student log, log to history, simple
Okay, so what we are doing here is we are just using a file class to put all the data into a file in case of this method is being called and in case of this method, we are putting data to control window
Now, what is easy, we have got two or three more sources where we want to put our data on conditional logic
Well, the smart way to handle this situation or maybe to tackle or to tackle with this problem is to pass any delegate here
So, okay, so how we can solve this problem using a delegate? Let's say, okay, we want, we don't want to print, we don't want to decide upon, okay, whether we want to put data to control window or we want to put data into a log file
Let's not decide it here. Let's just invoke any action method here
And we would say, okay, we are not deciding here, just pass any action method that takes students and do whatever we want
If you really want to take advantage of our action delegate, what we can do here is we should let not this message decide, okay, where we want to put our data
Okay, let's just reverse the code or undo the code that we have written and let's just pass any action method here that will take student as an input parameter and do whatever we want
Action to print. And this method says, okay, I want to call owner to decide upon where you want to put your data
I can just take any representative from the caller and I can just call that representative owner with this delegate by passing this object
Simple, right? So, this method accepts a delegate here. Now, from this method, we can call another method that is being passed from the caller method
So, this way, we can control where we want to put our data and that way, we will achieve one more principle that is OCP, open-close principle
So, we don't need to change our class again and again. We don't need to modify this method
So, let's see it in action. Now, this method is broken. Why it breaks
Because it needs an action parameter. So, let's create another action that's to student as an input parameter
Okay, let's take print to control action. And for lambda, okay, this will be an input parameter, let's say x
And what we want to do with this parameter, we want to put the data to control window
So, we want to put the data on control window. So, let's say x.name, x.age and x.address
And let's just pass print to control. Simple. That's doable. That's doable
As you can see, we can see our data on control window and we don't need to change our class
Now, after some days or maybe in some part of replication, we'll decide not to put our data on control window
We'll decide to put our data in a log file or in a text file. So, let's write another action method that's to student as an input parameter
And let me say print to log file. Okay, to put data to log file, let's use file and then write our text
Simple. And then just copy this line. First of all, where we want to put our data
Okay, let's put it into a file student log file.php and what we want to put inside the file
And now, instead of passing this print to control, let's pass it to log file and let's give it a go
As you can see, we are now seeing the data on control window
We haven't changed the data into database, but we are now seeing the data on control window
Let's look into the C drive. Student log file.php and there we have it
So, this is the advantage where we can follow OCP principles and we can pass our delegates to another class where we want to decide upon some kind of strategy or logic and we don't want to hard code that logic inside this method
So, that's a wrap for this video. We'll talk about function delegates in our next video