A delegate can be associated with a named method. When you instantiate a delegate by using a named method, the method is passed as a parameter, for example:
sample · csharpdocs/csharp/programming-guide/delegates/snippets/NamedAnonymousDelegates.csNamedDelegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateExamples;
class NamedDelegate
{
static NamedDelegate obj = new NamedDelegate();
//<NamedDelegate>
// Declare a delegate.
delegate void WorkCallback(int x);
// Define a named method.
void DoWork(int k) { /* ... */ }
// Instantiate the delegate using the method as a parameter.
WorkCallback d = obj.DoWork;
//</NamedDelegate>
}
class AnonymousDelegate
{
static AnonymousDelegate obj = new AnonymousDelegate();
//<SnippetAnonymousMethod>
// Declare a delegate.
delegate void WorkCallback(int x);
// Instantiate the delegate using an anonymous method.
WorkCallback d = (int k) => { /* ... */ };
//</SnippetAnonymousMethod>
}
//<DeclareAndUse>
// Declare a delegate
delegate void MultiplyCallback(int i, double j);
class MathClass
{
static void Main()
{
MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"
MultiplyCallback d = m.MultiplyNumbers;
// Invoke the delegate object.
Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{
d(i, 2);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Declare the associated method.
void MultiplyNumbers(int m, double n)
{
Console.Write(m * n + " ");
}
}
/* Output:
Invoking the delegate using 'MultiplyNumbers':
2 4 6 8 10
*/
//</DeclareAndUse>
//<AnotherSample>
// Declare a delegate
delegate void Callback();
class SampleClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}
class TestSampleClass
{
static void Main()
{
var sc = new SampleClass();
// Map the delegate to the instance method:
Callback d = sc.InstanceMethod;
d();
// Map to the static method:
d = SampleClass.StaticMethod;
d();
}
}
/* Output:
A message from the instance method.
A message from the static method.
*/
//</AnotherSample>The preceding example uses a named method. Delegates constructed with a named method can encapsulate either a static method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. C# enables you to instantiate a delegate and immediately specify a code block that the delegate processes when called. The block can contain either a lambda expression or an anonymous method, as shown in the following example:
sample · csharpdocs/csharp/programming-guide/delegates/snippets/NamedAnonymousDelegates.csSnippetAnonymousMethod
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateExamples;
class NamedDelegate
{
static NamedDelegate obj = new NamedDelegate();
//<NamedDelegate>
// Declare a delegate.
delegate void WorkCallback(int x);
// Define a named method.
void DoWork(int k) { /* ... */ }
// Instantiate the delegate using the method as a parameter.
WorkCallback d = obj.DoWork;
//</NamedDelegate>
}
class AnonymousDelegate
{
static AnonymousDelegate obj = new AnonymousDelegate();
//<SnippetAnonymousMethod>
// Declare a delegate.
delegate void WorkCallback(int x);
// Instantiate the delegate using an anonymous method.
WorkCallback d = (int k) => { /* ... */ };
//</SnippetAnonymousMethod>
}
//<DeclareAndUse>
// Declare a delegate
delegate void MultiplyCallback(int i, double j);
class MathClass
{
static void Main()
{
MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"
MultiplyCallback d = m.MultiplyNumbers;
// Invoke the delegate object.
Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{
d(i, 2);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Declare the associated method.
void MultiplyNumbers(int m, double n)
{
Console.Write(m * n + " ");
}
}
/* Output:
Invoking the delegate using 'MultiplyNumbers':
2 4 6 8 10
*/
//</DeclareAndUse>
//<AnotherSample>
// Declare a delegate
delegate void Callback();
class SampleClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}
class TestSampleClass
{
static void Main()
{
var sc = new SampleClass();
// Map the delegate to the instance method:
Callback d = sc.InstanceMethod;
d();
// Map to the static method:
d = SampleClass.StaticMethod;
d();
}
}
/* Output:
A message from the instance method.
A message from the static method.
*/
//</AnotherSample>The method that you pass as a delegate parameter must have the same signature as the delegate declaration. A delegate instance can encapsulate either static or instance method.
Method groups with a single overload have a natural type. The compiler can infer the return type and parameter types for the delegate type:
var read = Console.Read; // Just one overload; Func<int> inferred
var write = Console.Write; // ERROR: Multiple overloads, can't chooseExamples
The following example is a simple example of declaring and using a delegate. Notice that both the delegate, MultiplyCallback, and the associated method, MultiplyNumbers, have the same signature
sample · csharpdocs/csharp/programming-guide/delegates/snippets/NamedAnonymousDelegates.csDeclareAndUse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateExamples;
class NamedDelegate
{
static NamedDelegate obj = new NamedDelegate();
//<NamedDelegate>
// Declare a delegate.
delegate void WorkCallback(int x);
// Define a named method.
void DoWork(int k) { /* ... */ }
// Instantiate the delegate using the method as a parameter.
WorkCallback d = obj.DoWork;
//</NamedDelegate>
}
class AnonymousDelegate
{
static AnonymousDelegate obj = new AnonymousDelegate();
//<SnippetAnonymousMethod>
// Declare a delegate.
delegate void WorkCallback(int x);
// Instantiate the delegate using an anonymous method.
WorkCallback d = (int k) => { /* ... */ };
//</SnippetAnonymousMethod>
}
//<DeclareAndUse>
// Declare a delegate
delegate void MultiplyCallback(int i, double j);
class MathClass
{
static void Main()
{
MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"
MultiplyCallback d = m.MultiplyNumbers;
// Invoke the delegate object.
Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{
d(i, 2);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Declare the associated method.
void MultiplyNumbers(int m, double n)
{
Console.Write(m * n + " ");
}
}
/* Output:
Invoking the delegate using 'MultiplyNumbers':
2 4 6 8 10
*/
//</DeclareAndUse>
//<AnotherSample>
// Declare a delegate
delegate void Callback();
class SampleClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}
class TestSampleClass
{
static void Main()
{
var sc = new SampleClass();
// Map the delegate to the instance method:
Callback d = sc.InstanceMethod;
d();
// Map to the static method:
d = SampleClass.StaticMethod;
d();
}
}
/* Output:
A message from the instance method.
A message from the static method.
*/
//</AnotherSample>In the following example, one delegate is mapped to both static and instance methods and returns specific information from each.
sample · csharpdocs/csharp/programming-guide/delegates/snippets/NamedAnonymousDelegates.csAnotherSample
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateExamples;
class NamedDelegate
{
static NamedDelegate obj = new NamedDelegate();
//<NamedDelegate>
// Declare a delegate.
delegate void WorkCallback(int x);
// Define a named method.
void DoWork(int k) { /* ... */ }
// Instantiate the delegate using the method as a parameter.
WorkCallback d = obj.DoWork;
//</NamedDelegate>
}
class AnonymousDelegate
{
static AnonymousDelegate obj = new AnonymousDelegate();
//<SnippetAnonymousMethod>
// Declare a delegate.
delegate void WorkCallback(int x);
// Instantiate the delegate using an anonymous method.
WorkCallback d = (int k) => { /* ... */ };
//</SnippetAnonymousMethod>
}
//<DeclareAndUse>
// Declare a delegate
delegate void MultiplyCallback(int i, double j);
class MathClass
{
static void Main()
{
MathClass m = new MathClass();
// Delegate instantiation using "MultiplyNumbers"
MultiplyCallback d = m.MultiplyNumbers;
// Invoke the delegate object.
Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
for (int i = 1; i <= 5; i++)
{
d(i, 2);
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Declare the associated method.
void MultiplyNumbers(int m, double n)
{
Console.Write(m * n + " ");
}
}
/* Output:
Invoking the delegate using 'MultiplyNumbers':
2 4 6 8 10
*/
//</DeclareAndUse>
//<AnotherSample>
// Declare a delegate
delegate void Callback();
class SampleClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}
class TestSampleClass
{
static void Main()
{
var sc = new SampleClass();
// Map the delegate to the instance method:
Callback d = sc.InstanceMethod;
d();
// Map to the static method:
d = SampleClass.StaticMethod;
d();
}
}
/* Output:
A message from the instance method.
A message from the static method.
*/
//</AnotherSample>