Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a programmatic way to identify .Net reserved words? [closed]

I am looking for reading .Net, C# reserved key words programmatically in VS 2015.

I got the answer to read C# reserved words in the [link][1].

CSharpCodeProvider cs = new CSharpCodeProvider();
var test = cs.IsValidIdentifier("new"); // returns false
var test2 = cs.IsValidIdentifier("new1"); // returns true

But for var, dynamic, List, Dictionary etc the above code is returning wrong result.

Is there any way to identify .net keywords in run time instead of listing key words in a list?

string[] _keywords = new[] { "List", "Dictionary" };
like image 507
DSP Avatar asked May 31 '16 05:05

DSP


People also ask

Can we use reserved keywords as identifiers in C#?

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix.


1 Answers

This is a perfectly fine C# program:

using System;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int var = 7;
            string dynamic = "test";
            double List = 1.23;

            Console.WriteLine(var);
            Console.WriteLine(dynamic);
            Console.WriteLine(List);
        }
    }
}

So your premise is wrong. You can find the keywords by looking them up in the short list. Just because something has a meaning does not mean it's in any way "reserved".

Do not let the online syntax highlighting confuse you. Copy and paste it into Visual Studio if you want to see proper highlighting.

like image 142
nvoigt Avatar answered Sep 24 '22 06:09

nvoigt