While reading a book on C#, I have come across code that uses the @ to "overload" or use a C# keyword as an identifier. I am guessing this is not a good practice, as it leads to ambiguity. Am I correct in thinking this, or are there times where this should be used ?
Indeed, it is almost always worth avoiding this. The main valid time it might be useful is when dealing with an external library (from another language) that has a class / etc that happens to be a C# keyword, but I'll admit I've never had this problem in reality. It helps that C# allows you to rename method argument names when overriding / implementing interfaces, so that avoids one common set of cases.
Another (arguably lazy) use is in code generation; if you always prefix fields / variables / etc with @theName, then you don't have to worry about special cases. Personally, I simply cross-check against a list of C# keywords / contextual keywords, and add something like underscore.
The @ symbol's other use (verbatim string literals) is @"much more useful".
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