How to Resolve Error "CS0228: 'type' does not contain a definition for 'member', or it is not accessible" in C#
The Compiler Error CS0228 is a member lookup error. The message reads: "'Type' does not contain a definition for 'Member', or it is not accessible".
This error is a "catch-all" for two distinct problems:
- Missing Member: The method, property, or field simply does not exist on the type you are referencing (often a typo).
- Hidden Member: The member exists, but its Access Modifier (like
privateorprotected) prevents you from seeing it from your current location.
This guide explains how to diagnose whether the member is missing or just hidden, and how to fix the issue.
Understanding Member Lookup
When you type myObject.DoSomething(), the compiler looks at the data type of the variable myObject. It searches that class definition for a public member named DoSomething.
If it cannot find an exact match, or if it finds a match that is marked private, it raises CS0228 (or the related errors CS0117/CS0122), effectively saying: "I can't find a usable member with that name."
Scenario 1: Typos and Case Sensitivity
C# is case-sensitive. Calculate, calculate, and CALCULATE are three completely different names. This is the most common cause of this error.
Example of error
public class Calculator
{
public int Add(int a, int b) => a + b;
}
public class Program
{
static void Main()
{
var calc = new Calculator();
// ⛔️ Error CS0228 (or CS0117): 'Calculator' does not contain a definition for 'add'.
// The method is defined as 'Add' (Capital A).
int result = calc.add(5, 5);
}
}
Solution: Fix the Spelling
Ensure the casing matches the definition exactly. Use IntelliSense (Ctrl+Space) to verify the name.
public class Program
{
static void Main()
{
var calc = new Calculator();
// ✅ Correct: Capital 'A' matches the class definition
int result = calc.Add(5, 5);
}
}
Scenario 2: Access Modifier Restrictions
Sometimes the member exists and is spelled correctly, but it is marked private or protected. The error message "or it is not accessible" hints at this specific problem.
Example of error
Trying to access a private helper method from another class.
public class User
{
// This method is private (default)
void ResetPassword()
{
// ... logic ...
}
}
public class Admin
{
public void FixUser(User u)
{
// ⛔️ Error CS0228 (or CS0122): 'User.ResetPassword' is not accessible.
// You cannot call private methods from outside the class.
u.ResetPassword();
}
}
Solution: Update Visibility
If the method is intended to be used by others, change the modifier to public or internal.
public class User
{
// ✅ Correct: Now public, so other classes can call it.
public void ResetPassword()
{
// ... logic ...
}
}
If the member is protected, it can only be accessed by the class itself or classes that inherit from it.
Scenario 3: Polymorphism and Variable Types
The compiler checks the Variable Type, not the Runtime Object Type. If you store a specific object (like String) inside a generic container (like Object), you can only access members defined on the container type.
Example of error
Attempting to access a property of the subclass when the variable is typed as the base class.
public class Program
{
static void Main()
{
// 'obj' is defined as type 'object'
object obj = "Hello World";
// ⛔️ Error CS0228 (or CS0117): 'object' does not contain a definition for 'Length'.
// Even though "Hello World" has a Length, the variable 'obj' does not know about it.
Console.WriteLine(obj.Length);
}
}
Solution: Cast the Variable
You must tell the compiler to treat the variable as the specific type.
public class Program
{
static void Main()
{
object obj = "Hello World";
// ✅ Correct: Cast the object to string first
if (obj is string str)
{
// Now we are accessing the 'string' type, which has Length.
Console.WriteLine(str.Length);
}
}
}
Output:
11
Conclusion
CS0228 means the compiler cannot connect your call to a valid destination.
- Check the Name: Is it spelled correctly? Is the casing (Capitalization) correct?
- Check the Visibility: Is the member
private? Make itpublicif necessary. - Check the Type: Are you holding the object in a
varor base class variable that hides the member you want? Cast it to the correct type.