Compiler error ยท error CS1955
Non-invocable member 'X' cannot be used like a method
You added () to something that isn't a method. Nine times out of ten it's a property โ Count, Length, Name โ read like a field, not called like a function.
See it happen
var names = new List();
names.Add("Ada");
Console.WriteLine(names.Count()); // Count is a property โ no parentheses
Fix: drop the parentheses
var names = new List();
names.Add("Ada");
Console.WriteLine(names.Count); // property: read it, don't call it
The rule that decides it
Things that DO something are methods (with ()); things that ARE something are properties (without).
| Member | Property or method? | Correct use |
|---|---|---|
List<T>.Count | property | list.Count |
string.Length | property | s.Length |
T[].Length | property | arr.Length |
List<T>.Add(x) | method | list.Add(5) |
string.ToUpper() | method | s.ToUpper() |
Your own members: same rule
class Person
{
public string Name { get; set; } // property โ read as person.Name
public string Describe() // method โ call as person.Describe()
{
return Name;
}
}
Person p = new Person { Name = "Ada" };
Console.WriteLine(p.Name); // no ()
Console.WriteLine(p.Describe()); // with ()
Related, but different
| Code | Meaning |
|---|---|
CS1955 | Parentheses on a non-method (property/field) |
| CS1061 | The member name doesn't exist at all |
| CS1501 | The method exists but the argument count is wrong |
So if the compiler says "non-invocable", the name is right and the () is wrong. That narrows it to one character.
Frequently asked
What does CS1955 mean?
You used () on a member that is not a method โ usually a property like Count or Length. Remove the parentheses.
Property or method โ how do I tell?
Does it act (Add, ToUpper)? Method, with (). Does it simply have a value (Count, Name)? Property, no (). IntelliSense shows the distinction with a wrench vs. a cube icon.
Checklist
- Look at the name โ does it describe an action or a value?
- Actions:
(). Values: no(). - In Visual Studio, hover the member: IntelliSense says "property" or "method".
- Remember
Count(List) vsLength(array/string) โ both properties.