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

What are the properties of a string class?

0 Answers  


What is parseexact c#?

0 Answers  


How do you specify a custom attribute for the entire assembly (rather than for a class)?

0 Answers  


How do you inherit from a class in C#?

1 Answers  


What is difference between events and delegates?

0 Answers  


Distinguish between the Debug class and Trace class with its functionality?

0 Answers   Siebel,


Explain how obfuscator works in .net

0 Answers  


In howmany ways can you deploy an assembly?

0 Answers   Siebel,


Why generics are used in c#?

0 Answers  


How to run the program at particular time? It should run everyday at 3:00 PM. After executing the program should sleep until next day at 3:00 PM. Please explain with code?

5 Answers   Sanat, Wipro,


What is the property of a class in c#?

0 Answers  


What is the .net datatype that allows the retrieval of data by a unique key?

0 Answers  


Categories