How to Resolve Error "CS0117: 'type' does not contain a definition for 'identifier'" in C#
The Compiler Error CS0117 is a "Missing Member" error. The message reads: "'MyClass' does not contain a definition for 'MyMethod'".
This error occurs when you attempt to access a field, property, or method that the compiler simply cannot find within the specified class or struct. While this is often caused by simple typos, it can also stem from missing library references (namespaces) or attempting to access members that exist on a derived class but not on the base class variable holding it.
This guide covers the most common reasons your code cannot find the member you are looking for.
Scenario 1: Typos and Case Sensitivity
C# is a case-sensitive language. To the compiler, ToString, tostring, and TOSTRING are three completely different names. If you misspell a method name or get the casing wrong, the compiler will search the class, find no exact match, and throw CS0117.
Example of Error
public class User
{
public string Name { get; set; }
}
public class Program
{
static void Main()
{
var user = new User { Name = "Alice" };
// ⛔️ Error CS0117: 'User' does not contain a definition for 'name'
// The property is 'Name' (capital N), but we typed 'name'.
Console.WriteLine(user.name);
}
}
Solution: Match the Definition
Ensure the casing matches the definition exactly. Use IntelliSense (Ctrl + Space) to verify the name.
public class Program
{
static void Main()
{
var user = new User { Name = "Alice" };
// ✅ Correct: Capital 'N' matches the property definition.
Console.WriteLine(user.Name);
}
}
Scenario 2: Missing Extension Methods (LINQ)
This is a very frequent cause. Methods like .Where(), .ToList(), .First(), or .Select() are not actually members of the List<T> or Array classes. They are Extension Methods defined in the System.Linq namespace.
If you try to use them without importing that namespace, the compiler looks inside the List class, doesn't find them, and raises CS0117.
Example of error
using System.Collections.Generic;
// Missing: using System.Linq;
public class DataProcessor
{
public void Process()
{
List<int> numbers = new List<int> { 1, 2, 3 };
// ⛔️ Error CS0117: 'List<int>' does not contain a definition for 'First'
// The compiler cannot see the extension method because the namespace is missing.
int top = numbers.First();
}
}
Solution: Add the using Directive
Add the necessary namespace to the top of your file.
using System.Collections.Generic;
using System.Linq; // ✅ Correct: Imports LINQ extension methods
public class DataProcessor
{
public void Process()
{
List<int> numbers = new List<int> { 1, 2, 3 };
// ✅ Correct: .First() is now available.
int top = numbers.First();
}
}
Scenario 3: Polymorphism and Casting
When you store an object in a variable of a base type (e.g., object or a parent class), you can only access members defined in that base type, even if the underlying object is actually a derived class containing more methods.
Example of error
Attempting to access a property specific to string while the variable is stored as an object.
public void CheckData()
{
// Storing a string inside a generic object variable
object data = "Hello World";
// ⛔️ Error CS0117: 'object' does not contain a definition for 'Length'
// The class 'System.Object' does not have a Length property.
// The compiler strictly checks the variable type, not the runtime value.
Console.WriteLine(data.Length);
}
Solution: Cast to the Correct Type
You must explicitly tell the compiler to treat the variable as the specific type (string) or use Pattern Matching.
public void CheckData()
{
object data = "Hello World";
// ✅ Correct: Pattern matching checks the type and casts it to 'str'
if (data is string str)
{
Console.WriteLine(str.Length);
}
// Output: 11
}
Scenario 4: Outdated References (The Clean/Rebuild Fix)
Sometimes, CS0117 appears when your code is correct, but your project structure is out of sync.
If Project A references Project B, and you add a new method to Project B, Project A might fail to see it immediately if the build for Project B failed or hasn't run yet. Project A is looking at an old version of ProjectB.dll where the method doesn't exist yet.
Solution:
- Rebuild the Dependency: Right-click the referenced project (Project B) and select Build.
- Clean Solution: Go to Build > Clean Solution, then Rebuild Solution.
This ensures that the DLL referenced by your main project is physically updated with the latest metadata containing your new method.
Conclusion
CS0117 means the compiler searched the class and came up empty-handed.
- Check Spelling: Verify case-sensitivity (e.g.,
Namevsname). - Check Namespaces: If using LINQ or generic extensions, ensure
using System.Linq;is present. - Check Types: If accessing a child member via a parent variable, cast it first.
- Rebuild: If the method definitely exists in a referenced project, try a Clean/Rebuild to update the DLLs.