문자열이 유효한 키워드인지 확인하려면 IsValidIdentifier 메서드를 사용하세요.
IsValidIdentifier 메서드는 입력된 값이 식별자인지 여부를 확인합니다. 식별자가 아니면 C#의 키워드입니다.
CodeDomProvider를 설정하고 IsValiddentifier 메서드로 작업한 예를 살펴보겠습니다.
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#"); 전체 코드를 살펴보겠습니다L
예
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);
}
}
}
} 출력
amit is an identifier for is a Valid Keyword in C#