Delegates are often compared to indicators in functions in C or C ++, while it is necessary to remember about many rules at the pointers, using delegates is much simpler.
For example, if you want a user to enter a value of 1 or 0 when you enter 1, you will see an inscription on the screen “Binary one” and when 0 “Binary zero” of course, you can use the if statement but for larger projects the delegates are more practical.
Construction of a delegate
That’s how the delegates are created, just add a delegate, like a method only without a body. Example:
public delegate void binary();
Let’s create a program in the Main function, which we described at the beginning of the lesson, it will look like this:
namespace exercises
{
public delegate void binary();
class Program
{
static void Main(string[] args)
{
binary BinarySystem;
int number = int.Parse(Console.ReadLine());
if (number == 1)
{
BinarySystem = DisplayOne;
}
else if (number == 0)
{
BinarySystem = DisplayZero;
}
else
{
BinarySystem = DisplayException;
}
BinarySystem();
Console.ReadKey();
}
static void DisplayOne()
{
Console.WriteLine("Binary one");
}
static void DisplayZero()
{
Console.WriteLine("Binary zero");
}
static void DisplayException()
{
Console.WriteLine("This number doesn't exist in the binary system!");
}
}
}
And what do we have here? First, we created a delegate then its object in the Main function, then we retrieve data from the user and depending on what the user have written, we saving the appropriate method to the delegate, then we call the delegate.
And now let’s create more methods in the Main function:
static void DisplayExample()
{
Console.WriteLine("Call this function for an example!");
}
static void DisplayComplaining()
{
Console.WriteLine("And do not complain about the lack of an idea!");
}
And let’s add this function to the delegate and call it:
BinarySystem += DisplayExample;
BinarySystem += DisplayComplaining;
BinarySystem();
This is whole sense of the delegates, if we want to call a larger number of functions, we can just use the delegate.
Events
Events are very connected with delegates, they are used during the program operation, eg in WPF, what do you think about why a specific method is called after clicking a button?
Let’s look at the method of calling the button event:
private void Button_Click(object sender, RoutedEventArgs e)
{
//some code
}
This is the button click event.
Now let’s see how events work with delegates in the console.
class EventClass
{
public delegate void MyDelegate(int number,int secondNumber);
public event MyDelegate MyAction;
public int ReturnFirstNumber(int number)
{
return number*2;
}
public int ReturnSecondNumber(int secondNumber)
{
return secondNumber*2;
}
public int ReturnSum()
{
MyAction(ReturnFirstNumber(12), ReturnSecondNumber(34));
return ReturnFirstNumber(12) + ReturnSecondNumber(34);
}
}
class Program
{
static void Main(string[] args)
{
EventClass @event= new EventClass();
@event.MyAction += delegate(int number, int secondNumber)
{
Console.WriteLine("We have the result after calling the delegate: \nFirst number: "+ number+"\nSecond number: "+secondNumber);
};
Console.WriteLine("The result and launch of the event is due to calling the method 'event.ReturnSum()' = " + event.ReturnSum());
Console.ReadKey();
}
}
The most important in this is that the event is called in the class EventClass and the arguments are passed to the delegate in the Main function and displayed in the console. Below the result:
This content also you can find on my blog http://devman.pl/csharplan/c-language-15-events-delegates/
If you recognise it as useful, share it with others so that others can also use it.
Leave upvote and follow and wait for next articles :) .
That’s all in the next lesson will be dictionaries, see you!