List of C # keywords

I am trying to find a way to list all the C # keywords. I need to do a comparison like:

if (key == "if" || key == "while" || <further_comparisons>)
{
     // do something
}

      

It would be better to do this by searching the list of these keywords, and I would like to do this without typing them.

I am going through the System.CodeDom namespace to see if I can find something.

If any of you could tell me where I can find it, I would really appreciate it. Thank you in advance!

+3


source to share


4 answers


you can use

using Microsoft.CSharp;

 CSharpCodeProvider cs = new CSharpCodeProvider();

      



then you can use

var test = cs.IsValidIdentifier("if") //return false

      

+11


source


You will find a list of all keywords in the documentation: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/index



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

      

+1


source


CSharpCodeProvider

has logic for this. But you have to call it reflection. It contains a function IsKeyword

. More specifically, it has an actual list of keywords it uses IsKeyword

.

private static readonly string[][] keywords

      

+1


source


If you don't mind using reflection and depending on implementation details, you can use the static method of the IsKeyWord

Microsoft.CSharp.CSharpCodeGenerator Class .

using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

internal class CS
    {
    private static MethodInfo methIsKeyword;
    static CS()
        {
        using (CSharpCodeProvider cs = new CSharpCodeProvider())
            {
            FieldInfo infoGenerator = cs.GetType().GetField("generator", BindingFlags.Instance | BindingFlags.NonPublic);

            object gen = infoGenerator.GetValue(cs);
            methIsKeyword = gen.GetType().GetMethod("IsKeyword", BindingFlags.Static | BindingFlags.NonPublic);
            }
        }
    public static bool IsKeyword(string input)
        {
        return Convert.ToBoolean(methIsKeyword.Invoke(null, new object[] { input.Trim() }));
        }
    }

      

Usage example:

bool isKeyword = CS.IsKeyword("if");

      

0


source







All Articles