coding for delegates?

Answers were Sorted based on User's Feedback



coding for delegates?..

Answer / m.shanmuga sundaram

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public delegate int execute(int a, int
b); //declare a delegate method

static void Main(string[] args)
{
Program p = new Program();

execute ex = new execute(p.add);
Console.WriteLine(ex(2,3)); //add method is
executed

ex = new execute(p.subtract);
Console.WriteLine(ex(2, 3)); //subtract method
is executed
}

public int add(int a, int b)
{
return (a + b);
}

public int subtract(int a, int b)
{
return (a - b);
}
}
}

Is This Answer Correct ?    3 Yes 0 No

coding for delegates?..

Answer / 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

coding for delegates?..

Answer / navin c. pandit

Here is the code how to add a delegate:-

Button_Id.OnClick += new EventHandler(function_name());

where function_name() is the name of the method that you
want to call.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

How do you create dlls in .NET

0 Answers   Digital GlobalSoft,


How do you pass reference parameters in c#?

0 Answers  


What does it meant to say “the canonical” form of XML ?

1 Answers  


What is array and types of array in c#?

0 Answers  


What is sqlcommandbuilder c#?

0 Answers  






How do I create a multi language, multi file assembly?

0 Answers  


Why cannot you specify the accessibility modifier for methods inside the interface?

0 Answers  


the selected inf of other combobox?How do you achieve it?

1 Answers   CTS, Microsoft,


Is unboxing an implicit conversion?

0 Answers  


Why do we need to serialize data?

0 Answers  


How to create events for a control? What is custom events? How to create it?

0 Answers  


Define c# i/o classes?

0 Answers  


Categories