- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a string is a valid keyword in C#?
To check if a string is a valid keyword, use the IsValidIdentifier method.
The IsValidIdentifier method checks whether the entered value is an identifier or not. If it’s not an identifier, then it’s a keyword in C#.
Let us see an example, wherein we have set the CodeDomProvider and worked with the IsValiddentifier method −
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
Let us see the complete codeL
Example
using System; using System.IO; 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.Write("{0} is a Valid Keyword in C#", str2); } } } }
Output
amit is an identifier for is a Valid Keyword in C#
- Related Articles
- How to check if a string is a valid keyword in Python?
- How to check if a string is a valid keyword in Java?
- Check if a given string is a valid number in C++
- How to check if a string is a valid URL in Golang?
- Java Program to check if a string is a valid number
- C program to check if a given string is Keyword or not?
- Check if a given string is a valid number in Python
- Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++
- Python program to check if a given string is Keyword or not
- C Program to check if a date is valid or not
- How to check if a C/C++ string is an int?
- How to check if a given word is a Python keyword or not?
- Check If Word Is Valid After Substitutions in C++
- Check whether a string is valid JSON or not in Python
- Check whether the given string is a valid identifier in Python

Advertisements