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
Related
This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
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:
To underscore or to not to underscore, that is the question
(15 answers)
Closed 9 years ago.
We are following Hungarian notation to declare private variables as below.
private IRepository<Request> _requestRepo;
public RequestService(IRepository<Request> requestRepo)
{
_requestRepo = requestRepo;
}
The above code shows a sonar issue like Field name does not start with underscore. Please tell me how to declare the private variables to satisfy coding standards?
Please don't use hungarian notation for C#, it's very, very obsolete. Use the Microsoft Coding Guidelines. The whole .NET world is designed with those in mind, going against it will make your code stick out like a sore thumb.
That said, the guidelines aren't clear about rules for private fields. It's common to either use an underscore or camelCase. If you use Microsoft Visual Studio, use the Code Analysis feature (formerly known as FxCop) to get hints like make your private variable readonly in this case.
Your error message does not make any sense. If the message says your field name should not start with an underscore, I'd suggest camelCase as per the Microsoft Guidelines.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parsing CSV files in C#
I have a C# application that parses a pipe delimited file. It uses the Regex.Split method:
Regex.Split(line, #"(?<!(?<!\\)*\\)\|")
However recently a data file came across with a pipe included in one of the data fields. The data field in question used quoted identifers so when you open in Excel it opens correctly.
For example I have a file that looks like:
Field1|Field2|"Field 3 has a | inside the quotes"|Field4
When I use the above regex it parses to:
Field1
Field2
Field 3 has a
inside the quotes
Field4
when I would like
Field1
Field2
Field 3 has a | inside the quotes
Field4
I've done a fair amount of research and can't seem to get the Regex.Split to split the file on pipes but respect the quoted identifiers. Any help is greatly appreciated!
Here is a quick expression I've thrown together than seems to do the trick:
"([^"]+)"|([^\|]+)
Though your expression seems to be doing something with \'s as well, so you might need to add to this expression any other needs you have. I've ignored them in my answer because they were not explained in the question and therefore I cannot provide a solution without knowing why they are there - they may in fact not need to be there at all.
Also, my expression ignores empty fields though (i.e. 1||2|3 would come out as 1, 2 and 3 only) and I don't know whether this is what you need, if it isn't let me know and I can change the expression to something that would cater for that too.
Hope this helps anyway.