Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In the past blog: https://bit.ly/csharpdelegatessimply![]()
Now let’s get methodically anonymous, see, methods and anonymous, I really crack me up.
In our previous Lambda, Lambdee, Lambduh blog, I may have used code that I read from someone else's blog but can’t remember who, so please forgive me whoever it is, maybe leave a comment!
Ok, how is the anonymous expression used by delegates?
When the C# Compiler finds the delegate keyword inside of a method body, it expects that the delegate keyword will be followed by an anonymous body. Memorize that. Figure out why later. It’ll come with experience.
In the following code I use the anonymous approach, but the way that I used the braces was to show that the anonymous methods are exactly that: Methods.
Code Snippet
- using System;
- namespace SimplestDelegate
- {
- /**************************************************
- * Declare the delegate
- *************************************************/
- public delegate void DelegateSimply();
- class Program
- {
- static void Main(string[] args)
- {
- /**************************************************
- * Anonymous and delegate
- *************************************************/
- DelegateSimply delegateInstance = delegate()
- {
- string Ano = "Anonymous function first";
- Console.BackgroundColor = ConsoleColor.Cyan;
- Console.WriteLine(Ano);
- };
- /**************************************************
- * Use of the += attacher operator is shown
- *************************************************/
- delegateInstance += delegate()
- {
- string Ano = "Anonymous function second";
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.WriteLine(Ano);
- };
- /*********End of Anonymous and Delegate************/
- /*********Start of Delegate only*******************
- * Delegate, not anonymous follows
- /**************************************************
- * Instantiate the delegate variable,
- * pass the method
- *************************************************/
- DelegateSimply delSimply = new DelegateSimply(method1);
- //Gingerbread decoration
- Console.BackgroundColor = ConsoleColor.Blue;
- /**************************************************
- * Invocate the delegate
- *************************************************/
- delSimply();
- /**************************************************
- * Second instantiation for method 2
- *************************************************/
- delSimply = method2;
- //Gingerbread decoration
- Console.BackgroundColor = ConsoleColor.Red;
- /**************************************************
- * Invocate the second instantation for method2
- *************************************************/
- delSimply();
- /***********End of delegate***********************/
- //Gingerbread decoration
- Console.BackgroundColor = ConsoleColor.Black;
- //
- }
- public static void method1()
- {
- Console.WriteLine("method1 was called by a delegate!");
- }
- public static void method2()
- {
- Console.WriteLine("method2 was called by a delegate!");
- }
- }
- }