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

Explain how many types of exception handlers are there in .net?

477


What is private void in c#?

526


Why attributes are used in c#?

477


What are most desktop applications written in?

479


How many digits is a 32 bit number?

479






What is the major difference between a custom control and user control?

561


What do you understand by 'access specifiers' in C#?

524


What is code verification?

537


Name any three ways to pass parameters to a method in c#?

490


What are collection classes?

485


Are c# destructors the same as c++ destructors?

563


What is an assembly qualified name

526


In which way a two-dimensional array declared in C#?

510


Explain the role of Garbage collector and its generations?

523


Is php easier than c#?

497