CS1955 – Non-invocable member ‘xyz’ cannot be used like a method

Loading

The error CS1955 – Non-invocable member 'xyz' cannot be used like a method occurs in C# when you attempt to use a non-method member (such as a property, field, or event) as if it were a method. This typically happens when you mistakenly add parentheses () after a member that is not callable.

Common Causes and Solutions

  1. Using a Property or Field as a Method:
    If you try to invoke a property or field as if it were a method, this error will occur.
   public class Example
   {
       public int Value { get; set; }
   }

   var example = new Example();
   example.Value(); // Error: CS1955 – Non-invocable member 'Value' cannot be used like a method

Fix: Remove the parentheses and use the property or field correctly.

   example.Value = 10; // Correct usage of the property
  1. Using an Event as a Method:
    Events cannot be invoked directly like methods. If you try to do so, this error will occur.
   public class Example
   {
       public event Action MyEvent;
   }

   var example = new Example();
   example.MyEvent(); // Error: CS1955 – Non-invocable member 'MyEvent' cannot be used like a method

Fix: Invoke the event only if it has subscribers, and use the proper event invocation syntax.

   example.MyEvent?.Invoke(); // Correct way to invoke an event
  1. Using a Delegate Field Without Invocation:
    If you have a delegate field and forget to invoke it, this error can occur.
   public class Example
   {
       public Action MyAction;
   }

   var example = new Example();
   example.MyAction = () => Console.WriteLine("Hello");
   example.MyAction; // Error: CS1955 – Non-invocable member 'MyAction' cannot be used like a method

Fix: Invoke the delegate using parentheses.

   example.MyAction(); // Correct way to invoke a delegate
  1. Confusing Methods with Properties:
    If you mistakenly treat a method as a property or vice versa, this error can occur.
   public class Example
   {
       public int GetValue() => 42;
   }

   var example = new Example();
   var value = example.GetValue; // Error: CS1955 – Non-invocable member 'GetValue' cannot be used like a method

Fix: Use the method with parentheses to invoke it.

   var value = example.GetValue(); // Correct way to call a method
  1. Using Indexers Incorrectly:
    If you try to use an indexer without the proper syntax, this error can occur.
   public class Example
   {
       public int this[int index] => index * 2;
   }

   var example = new Example();
   example[0]; // Error: CS1955 – Non-invocable member 'this[]' cannot be used like a method

Fix: Use the indexer with the correct syntax.

   var value = example[0]; // Correct way to use an indexer

Leave a Reply

Your email address will not be published. Required fields are marked *