C# Error
CS1061 – ‘{0}’ does not contain a definition for ‘{1}’ and no accessible extension method ‘{1}’ accepting a first argument of type ‘{0}’ could be found (are you missing a using directive or an assembly reference?)
Reason for the Error & Solution
‘type’ does not contain a definition for ‘name’ and no accessible extension method ‘name’ accepting a first argument of type ‘type’ could be found (are you missing a using directive or an assembly reference?).
This error occurs when you try to call a method or access a class member that does not exist.
Example
The following example generates CS1061 because Person
does not have a DisplayName
method. It does have a method that is called WriteName
. Perhaps that is what the author of this source code meant to write.
public class Person
{
private string _name;
public Person(string name) => _name = name;
// Person has one method, called WriteName.
public void WriteName()
{
System.Console.WriteLine(_name);
}
}
public class Program
{
public static void Main()
{
var p = new Person("PersonName");
// The following call fails because Person does not have
// a method called DisplayName.
p.DisplayName(); // CS1061
}
}
To correct this error
- Make sure you typed the member name correctly.
- If you have access to modify this class, you can add the missing member and implement it.
- If you don’t have access to modify this class, you can add an .