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" };
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With