How to check if a string is a valid keyword in C#?

To check if a string is a valid keyword in C#, you can use the IsValidIdentifier method from the CodeDomProvider class. This method determines whether a given string is a valid identifier or a reserved keyword in C#.

The IsValidIdentifier method returns true if the string is a valid identifier (like variable names, method names), and false if it's a reserved keyword (like for, if, class, etc.).

Syntax

Following is the syntax for using IsValidIdentifier method −

CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
bool isIdentifier = provider.IsValidIdentifier(stringToCheck);

Using IsValidIdentifier to Check Keywords

Here's how to create a provider and check if strings are keywords −

using System;
using System.CodeDom.Compiler;

namespace Program {
    class Demo {
        static void Main(string[] args) {
            string str1 = "amit";
            string str2 = "for";

            CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");

            // checking for str1
            if (provider.IsValidIdentifier(str1)) {
                Console.WriteLine("{0} is an identifier", str1);
            } else {
                Console.WriteLine("{0} is a Valid Keyword in C#", str1);
            }

            // checking for str2
            if (provider.IsValidIdentifier(str2)) {
                Console.WriteLine("{0} is an identifier", str2);
            } else {
                Console.WriteLine("{0} is a Valid Keyword in C#", str2);
            }
        }
    }
}

The output of the above code is −

amit is an identifier
for is a Valid Keyword in C#

Checking Multiple Keywords

You can also check multiple strings at once to identify which ones are keywords −

using System;
using System.CodeDom.Compiler;

namespace Program {
    class KeywordChecker {
        static void Main(string[] args) {
            string[] testStrings = { "class", "method", "int", "variable", "if", "else", "myVariable" };
            
            CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");

            Console.WriteLine("Keyword Check Results:");
            Console.WriteLine("---------------------");
            
            foreach (string str in testStrings) {
                if (provider.IsValidIdentifier(str)) {
                    Console.WriteLine("{0} - Valid Identifier", str);
                } else {
                    Console.WriteLine("{0} - C# Keyword", str);
                }
            }
        }
    }
}

The output of the above code is −

Keyword Check Results:
---------------------
class - C# Keyword
method - Valid Identifier
int - C# Keyword
variable - Valid Identifier
if - C# Keyword
else - C# Keyword
myVariable - Valid Identifier

How It Works

The CodeDomProvider class provides language-specific code generation and compilation services. When you call IsValidIdentifier, it checks the string against C#'s reserved keyword list and identifier naming rules.

Return Value Meaning Examples
true Valid identifier (not a keyword) myVariable, userName, Calculate
false Reserved keyword in C# class, int, for, if, while

Conclusion

The IsValidIdentifier method from CodeDomProvider is an effective way to check if a string is a reserved C# keyword. It returns false for keywords and true for valid identifiers, making it useful for dynamic code generation and validation scenarios.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements