Skip to main content

How to Resolve Error "CS1041: Identifier expected; 'name' is a keyword" in C#

The Compiler Error CS1041 is a naming conflict error. The message reads: "Identifier expected; 'int' (or other keyword) is a keyword".

In C#, an Identifier is the name you give to a variable, class, method, property, or parameter. Keywords are reserved words that define the language structure (e.g., class, namespace, if, void, int).

This error occurs when you try to use a reserved keyword as a name for your own code elements. The compiler gets confused because it interprets the word as a command or type definition rather than a name.

This guide explains how to avoid naming conflicts and how to force the use of keywords as names if absolutely necessary.

Understanding Keywords vs. Identifiers​

The C# compiler reserves roughly 80 words for its own use.

  • Keywords: abstract, as, base, bool, break, byte, case, catch, char, checked, class, const, continue, decimal, default, delegate, do, double, else, enum, event, explicit, extern, false, finally, fixed, float, for, foreach, goto, if, implicit, in, int, interface, internal, is, lock, long, namespace, new, null, object, operator, out, override, params, private, protected, public, readonly, ref, return, sbyte, sealed, short, sizeof, stackalloc, static, string, struct, switch, this, throw, true, try, typeof, uint, ulong, unchecked, unsafe, ushort, using, virtual, void, volatile, while.

If you try to name a variable float, the compiler thinks you are trying to declare a new floating-point type, not creating a variable named "float".

Scenario 1: Naming Variables with Keywords​

This is the most common occurrence. You want a variable to hold an event, so you name it event. Or you want a variable to hold a class type, so you name it class.

Example of error​

public void ProcessData()
{
// ⛔️ Error CS1041: Identifier expected; 'float' is a keyword
// The compiler sees: Type(float) Keyword(float) = Value(10.5).
// It expects a name where the second 'float' is.
float float = 10.5f;

// ⛔️ Error CS1041: 'event' is a keyword for declaring events.
string event = "Login";
}

Solution: Rename the Identifier​

The best practice is to choose a more descriptive name or use standard naming conventions (like camelCase).

public void ProcessData()
{
// ✅ Correct: Descriptive names
float myFloat = 10.5f;

// ✅ Correct: Using context
string eventName = "Login";
}

Scenario 2: Method Parameters and External APIs​

Sometimes you are forced to use a specific name. For example, you might be implementing an Interface defined in another language, or deserializing JSON data where the field name is strictly "class" or "namespace".

Example of error​

Trying to define a parameter using a reserved word.

public class Analytics
{
// ⛔️ Error CS1041: Identifier expected; 'namespace' is a keyword.
public void LogMetric(string namespace, int count)
{
}
}

Solution: Use the Verbatim Identifier (@)​

C# provides an escape character for identifiers: the At Symbol (@). Placing @ before a keyword tells the compiler: "Treat this word as a name, not a command."

public class Analytics
{
// ✅ Correct: '@namespace' is a valid identifier.
// When compiled, the variable name is simply "namespace" (without the @).
public void LogMetric(string @namespace, int count)
{
Console.WriteLine($"Metric for {@namespace}: {count}");
}
}
note

Interoperability: This feature is crucial when consuming libraries written in other .NET languages (like F# or VB.NET) that might allow identifiers that are keywords in C#.

Conclusion​

CS1041 means you stepped on a reserved word.

  1. Check the Word: Is the name highlighted in Blue or Purple in your IDE? That means it's a keyword.
  2. Preferred Fix: Rename the variable (e.g., event -> evt, class -> cls, int -> number).
  3. Mandatory Fix: If you must use that specific name (e.g., for JSON serialization or API compliance), prefix it with @ (e.g., @class).