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.
Related
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;
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:
What is the difference between Convert.ToBoolean(string) and Boolean.Parse(string)?
(3 answers)
Closed 6 years ago.
Why do Boolean.TryParse() and Convert.ToBoolean() evaluate a string differently?
I understand how they end up evaluating differently:
Boolean.TryParse() will match (case insensitive) 'true' and 'false'.
Convert.ToBoolean() will match to a whole range of values (example demonstrated in Microsoft doco linked above) which I would consider more natural.
Its the reasoning behind the difference I dont understand.
There are a couple of discussions touching on this subject which don't seem to address this particular question.
It's in the method/class names.
Convert -> you already have some value, you convert it to another type. e.g. you have value 1 which can be converted to true.
Parse -> you have the value as a string and you parse it.
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:
Why can't strings be mutable in Java and .NET?
(17 answers)
Closed 8 years ago.
What is meant by "strings are immutable in c#". I need some examples to understand this.I can not find some proper examples to understand this
This means that if you assign
string s = "Hello";
you cannot modify the string s. Thus, if you do
s = "Goodbye";
the literal "Hello" is not modified, but a new literal "Goodbye" is assigned to s.
Searching on your text "strings are immutable in c#" I find: http://msdn.microsoft.com/en-us/library/362314fe.aspx
Which seems to be saying that strings are never changed objects, they are always destroyed and re-created.
Per Microsoft:
Strings are immutable--the contents of a string object cannot be changed after the
object is created, although the syntax makes it appear as if you can do this.
For example, when you write this code, the compiler actually creates a new string object
to hold the new sequence of characters, and that new object is assigned to b. The string "h"
is then eligible for garbage collection.
string b = "h";
b += "ello";