Compiler Error CS0149
Method name expected
When creating a delegate, you must specify a method that matches the signature of the delegate. For more information, see How to: Declare, Instantiate, and Use a Delegate (C# Programming Guide).
The following sample generates CS0149:
using System;
namespace CompilerError0149
{
// MyDelegate's signature specifies a single integer parameter
// and returns a string.
delegate string MyDelegate(int i);
class MyClass
{
// Declare a field of the defined delegate type.
static MyDelegate dt;
public static void Main()
{
// The following delegate syntax requires an appropriate method
// for the argument, not a number.
dt = new MyDelegate(17.45); // CS0149
// Try the following line instead. This time the argument is a
// method that has a signature that matches the delegate.
//dt = new MyDelegate(ExampleFunction);
// The delegate can be sent as an argument to another method.
AnotherMethod(dt);
// Or it can be called directly.
dt(10);
}
// ExampleMethod has an integer parameter and returns a string.
public static string ExampleMethod(int j)
{
Console.Write("ExampleMethod, j = ");
Console.WriteLine(j);
return j.ToString();
}
public static void AnotherMethod(MyDelegate myDel)
{
myDel(8);
}
}
}
Change History
Date |
History |
Reason |
---|---|---|
June 2010 |
Expanded the example. |
Customer feedback. |