In your code sample, g1 and g2 are local variables in the Main function. They cannot be seen anywhere outside the main function. Since you're trying to use a variable named g1 in GoodMorning, you're getting an error.
Hopefully this will make more sense to you.
You declare a delegate method with no parameters. You want to assign two instances of this delegate and each will be assigned to a seperate method.
Each method must have the same parameter list as the delegate (which you do have).
Your attempt at assigning an event (+=) is totally wrong. Don't confuse event handlers with delegates. Delegates basically define a method call syntax. You can assign any method that has the same list of parameters to an instance of the delegate.
In the code snipet, I create an instance of Hello to a variable named g1. The delegate takes a single parameter which is the name of the method that will be called.
Then you invoke (call) the instance to cause the assigned method to be executed.
public delegate void Hello();
public delegate void Hello2(string value);
static void Main(string args)
// Assign g1 to the Goodmorning() method
Hello g1 = new Hello(Goodmorning);
// Assign g2 to the Goodevening() method
Hello g2 = new Hello(Goodevening);
Hello2 g3 = new Hello2(Salutations);
// Invoke both methods through the delegate
// Wait for the user to press a key to exit
Console.WriteLine("<press any key to exit>");
public static void Goodmorning()
public static void Goodevening()
public static void Salutations(string value)
Thank you all for the help.
its good discusing with you all and we the beginners become more comfortable with the computer tech.
my children love when we solve such little tasks with the computer on visual studio .
thanks guys jerryShaw and Rashikal Fol.