How to Resolve Error "CS0118: 'name' is a 'construct1' but is used like a 'construct2'" in C#
The Compiler Error CS0118 is a Semantic Mismatch error. For example, the message could state: " 'Name' is a 'namespace' (or type) but is used like a 'variable' (or type)."
C# distinguishes strictly between different kinds of constructs (Namespaces, Types, Methods, Variables, and Properties). This error occurs when you try to perform an operation on a symbol that is syntactically invalid for what that symbol actually represents. For example, you cannot "assign a value" to a Class name, nor can you "instantiate" a Namespace.
This guide explains the common mix-ups that trigger this error and how to correct your syntax.
Understanding Construct Mismatches
The compiler knows exactly what every name in your code represents.
Systemis a namespace.Stringis a type (class).xmight be a variable.
If you write System = 5;, the compiler complains: " 'System' is a 'namespace' but is used like a 'variable' ". You are trying to put data into a container that isn't designed to hold data.
Scenario 1: Using a Type like a Variable
This is the most common occurrence. It happens when you try to assign a value to a Class Name instead of a specific instance (object) of that class.
Example of error
You have a class named HtmlText, and you try to assign a string to it directly, confusing the Class definition with a Variable.
public class HtmlText
{
public string Content { get; set; }
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0118: 'HtmlText' is a 'type' but is used like a 'variable'
// You cannot assign a string to the Class definition itself.
HtmlText = "<div>Hello</div>";
}
}
Solution: Create an Instance
You must create a variable (an instance) of that type, and then assign the value to a property of that variable.
public class Program
{
static void Main()
{
// ✅ Correct: Create an object 'myText' of type 'HtmlText'
HtmlText myText = new HtmlText();
// Assign value to the object's property
myText.Content = "<div>Hello</div>";
}
}
Scenario 2: Using a Namespace like a Type
This occurs when you try to create an object using new, but the name you provide is a Namespace, not a Class.
Example of error
// Defined namespace
namespace MyHelpers.Math
{
public class Calculator { }
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0118: 'MyHelpers.Math' is a 'namespace' but is used like a 'type'
// You cannot create an instance of a namespace.
var math = new MyHelpers.Math();
}
}
Solution: Instantiate the Class
You must refer to the specific class inside the namespace.
public class Program
{
static void Main()
{
// ✅ Correct: Instantiate the 'Calculator' class found inside the namespace
var calc = new MyHelpers.Math.Calculator();
}
}
Scenario 3: Using a Method like a Type
This often happens when casting. You try to cast a variable using (MethodName) instead of (TypeName).
Example of error
public class Converter
{
public int ToInt() => 0;
public void Run()
{
double d = 10.5;
// ⛔️ Error CS0118: 'ToInt' is a 'method' but is used like a 'type'
// Parentheses usually mean 'Call Method' OR 'Cast'.
// Here, the syntax implies a Cast, but 'ToInt' is not a data type.
int i = (ToInt)d;
}
}
Solution: Fix the Syntax
If you meant to call the method, use standard method call syntax. If you meant to cast, use the actual type name.
public class Converter
{
public void Run()
{
double d = 10.5;
// ✅ Correct: Standard cast to a Type (int)
int i = (int)d;
}
}
Tip: If you see the message "'X' is a 'property' but is used like a 'method'", check if you accidentally added parentheses () to a property access: obj.Name() instead of obj.Name.
Conclusion
CS0118 is a categorization error. The compiler is telling you that you are holding the wrong tool for the job.
- Read the Error: Identify what the symbol is (e.g., namespace) and how you used it (e.g., type).
- Check Variable Names: Did you name a variable the same as a class? This can cause the compiler to resolve the Class name when you meant the Variable.
- Check Instantiation: Ensure you are
new-ing up a Class, not a namespace. - Check Assignment: Ensure you are assigning to a Variable, not a Class definition.