C uses anonymous methods to define the implementation of delegates
- 2021-07-03 00:45:08
- OfStack
In this paper, an example is given to illustrate the implementation method of C # defining delegates with anonymous methods. Share it for your reference. The specific implementation method is as follows:
// Defining delegates with anonymous methods
class Program
{
delegate string MyDelagate(string val);
static void Main(string[] args)
{
string str1 = " External to anonymous method ";
// The brackets are partially defined 1 Method, without a name, the compiler specifies 1 Names
MyDelagate my = delegate(string param)
{
string str2 = " Inside an anonymous method ";
return param + str1 + str2;
};
// Invoke an anonymous method of a delegate
Console.WriteLine(my(" Parameter "));
// It can be seen from the results that the anonymous method also achieves the effect of defining methods for delegates
Console.Read();
}
}
I hope this article is helpful to everyone's C # programming.