April 2018
Intermediate to advanced
300 pages
7h 41m
English
Delegates are a type in .NET which hold the reference to the method. The type is equivalent to the function pointer in C or C++. When defining a delegate, we can specify both the parameters that the method can take and its return type. This way, the reference methods will have the same signature.
Here is a simple delegate that takes a string and returns an integer:
delegate int Log(string n);
Now, suppose we have a LogToConsole method that has the same signature as the one shown in the following code. This method takes the string and writes it to the console window:
static int LogToConsole(string a) { Console.WriteLine(a);
return 1;
}
We can initialize and use this delegate like this:
Log logDelegate = LogToConsole; logDelegate ...