This question already has answers here:
List C# keywords
(5 answers)
Closed last year.
C# has a number of reserved keywords such as class, string, struct, ref and more.
A complete list of reserved keywords can be found here.
Is there a way to check a string for these reserved keywords, without manually maintaining a list of them?
The CSharpCodeProvider has a method IsValidIdentifier that accepts a string as input and will return false if the string is invalid for use as an identifier (including because it is a reserved keyword). Technically this will check more than just reserved keywords, but it is the one of the easiest ways to check against reserved keywords programmatically.
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
bool wordIsReserved = codeProvider.IsValidIdentifier("public") == false;
Related
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 1 year ago.
I was browsing sources for microsoft .netcore runtime and came across these lines of code
as you can see they are using # symbol infront of every error message getter like #Error_InvalidFilePath.
My question is, what is this language feature that is being used here?
And, Where can I read more about it?
Thanks
The # is a way to use reserved words as names. E.g. the variable class could be used as variable name like #class.
For non reserved names this won't add anything. But of course you don't know which names are reserved in the future. Your code example is generated code, which should preferably work for newer language versions and so the # makes sense there.
See docs
This question already has answers here:
How do I interpolate strings?
(15 answers)
Closed 5 years ago.
HttpResponseMessage response = await client.PutAsJsonAsync($"api/products/{product.Id}", product);
In the above code I've been using the $ keyword but I don't the significance of this keyword. I searched in google but coiuldn't find proper answer. I think this might be a duplicate but couldn't find relative answer even in stackexchange.
Thanks in advance
It's an interpolated string - the new feature of C# 6, which is basically just a syntax sugar for String.Format (compiler converts interpolated strings into the String.Format calls). Your string is equivalent to
String.Format("api/products/{0}", product.Id)
$"api/products/{product.Id}"
is the short version for
string.Format("api/products/{0}", product.Id);
You could have a look in the MSDN
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 5 years ago.
When I read a source code C#, I see a line
return new Form(ElementType.Checkbox, ((IHTMLInputElement)htmlElement).#checked);
But I can NOT find it with keyword .#checked.
Could you please tell me what is .#checked called and its meaning?
Update answer
Firstly, Thanks #Yeldar Kurmangaliyev, #Erik Funkenbusch and #TcKs.
Secondly, I'm sorry for making duplicate post. Because I searched with ".#checked" and saw nothing.
Finally, I understood that # sign help define a variable whose name is as same as C# keyword, like this:
string #string = "This is a string";
string str = #string; // str = "This is a string"
The checked is keyword in C# and therefore the name must be prefixed with # as documentation says:
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. For example, #if is a valid
identifier but if is not because if is a keyword.
So ((IHTMLInputElement)htmlElement).#checked) means accessing the checked member on instance of class implementing IHTMLInputElement.
This question already has answers here:
What's the use/meaning of the # character in variable names in C#?
(9 answers)
Closed 7 years ago.
So I was digging through some code and I saw something along these lines.
Func<T> #delegate = ...
My question is what is the # operator used for in this case?
I've seen it used when creating string literals but never when referencing something other than a string.
Func<T> is a generic delegate in C#.
delegate is a reserved word in C# so the developer prefixed it with # to use it as a valid variable name.
You will see it in asp.net MVC project for HTML helpers where for setting class we use #class as class is a reserved word in C# so we can't have a variable with name class
This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 7 years ago.
i have a semicolon separates string, that contains values of every type. string and date values are in quotations.
Now i have an evil string, where an inner string contains s semicolon, that i need to remove (replace by nothing).
eg:
"Value1";0;"Value2";4711;"Evil; Value";"2015-09-03"
in C#:
string value = "\"Value1\";0;\"Value2\";4711;\"Evil; Value\";\"2015-09-03\""
So how to replace all semicolons, that are in quotations? can anybody help?
Regex is awful at handling delimited strings. It can do it, but it's not often as good of a choice as it first appears. This is one of several reasons why.
Instead, you should use a dedicated delimited string parser. There are (at least) three built into the .Net framework. The TextFieldParser type is one of those, and it will handle this correctly.
You should try this i.e to match only those semicolons which is not preceded by : :
(?<=[^"]);
Here is demo