coding for delegates?

Answer Posted / dotnet user

using System;
// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);

class MainClass
{
// Regular method that matches signature:
static void SampleDelegateMethod(string message)
{
Console.WriteLine(message);
}

static void Main()
{
// Instantiate delegate with named method:
SampleDelegate d1 = SampleDelegateMethod;
// Instantiate delegate with anonymous method:
SampleDelegate d2 = delegate(string message)
{
Console.WriteLine(message);
};

// Invoke delegate d1:
d1("Hello");
// Invoke delegate d2:
d2(" World");
}
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can we set the class to be inherited, but prevent the method from being over-ridden?

656


Is c# an open source?

657


i want o/p 011242110 in c# code.

1839


What is the difference between ienumerable and iqueryable?

628


What do you know about WM_CHAR message?

726






What is parameters in c#?

674


Where are value types stored in c#?

718


What is ilist c#?

669


How to prevent the error while updating ui control from another thread?

687


What is a event in c#?

606


What does console readline do?

636


What are access modifiers used for?

782


How can I create a process that is running a supplied native executable (e.g., Cmd.exe)?

772


What is inline function in c#?

691


What does exclamation mark mean in access query?

744