Skip to main content

How to Resolve Error "CS0149: Method name expected" in C#

The Compiler Error CS0149 is a syntax error related to Delegate Instantiation. The message reads: "Method name expected".

This error occurs when you attempt to create a new instance of a delegate using the new keyword, but you provide something that is not a method name as the argument. Most commonly, this happens when you accidentally add parentheses () to the method name inside the delegate constructor, which invokes the method instead of passing its address.

This guide explains the correct syntax for initializing delegates.

Understanding Delegate Syntax

A delegate is a pointer to a method. When you initialize a delegate, you are telling it where the code is located, not executing the code immediately.

  • MyMethod: Refers to the method itself (the address/pointer).
  • MyMethod(): Executes the method and returns its result (a value).

The delegate constructor expects a Method Group (the name), not a Value (the result).

Scenario 1: Accidental Method Invocation

This is the primary cause of CS0149. You treat the argument like a standard function call.

Example of error:

public delegate void MyHandler();

public class Program
{
public static void DoWork()
{
System.Console.WriteLine("Working...");
}

static void Main()
{
// ⛔️ Error CS0149: Method name expected.
// 'DoWork()' calls the method. It returns 'void'.
// You cannot pass 'void' to a constructor expecting a function pointer.
MyHandler handler = new MyHandler(DoWork());
}
}

Solution 1: Remove Parentheses

To fix the error, simply remove the parentheses. This passes the method reference instead of invoking it.

public delegate void MyHandler();

public class Program
{
public static void DoWork()
{
System.Console.WriteLine("Working...");
}

static void Main()
{
// ✅ Correct: 'DoWork' without parentheses is the method name.
MyHandler handler = new MyHandler(DoWork);

// Now invoke the delegate
handler();
}
}

Solution 2: Method Group Conversion (Simplification)

Since C# 2.0, you generally do not need the new DelegateType(...) syntax at all. You can assign the method name directly to the variable. This is cleaner and less prone to syntax errors.

For example:

public delegate void MyHandler();

public class Program
{
public static void DoWork() { }

static void Main()
{
// ✅ Recommended: Direct assignment
// The compiler infers 'new MyHandler(DoWork)' automatically.
MyHandler handler = DoWork;
}
}
note

Lambda Expressions: You can also resolve this error by using a lambda expression if you are trying to run inline code: MyHandler handler = () => DoWork();

Conclusion

CS0149 is a "pointer vs. value" confusion.

  1. Check Arguments: Look inside new Delegate(...).
  2. Remove Parentheses: If you see Method(), change it to Method.
  3. Simplify: Consider removing new Delegate(...) entirely and just using direct assignment.