Stupid Question 70: Verbatim identifiers in C#, should they be used and when?
verbatim identifier, when and should they be used?
I know there is this thing called verbatim identifiers, basically you add an @ in front of a reserved C# keyword and you are free to use it as a variable.
Have a look at this msdn example:
[sourcecode language=“csharp”]
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine(“true”);
else
System.Console.WriteLine(“false”);
}
}
[/sourcecode]
Pretty messed up right? I agree. The use of verbatim identifiers is not recommended, but I learned that it can be useful in legacy code (keywords introduced later), working with other languages or with libraries where you need to use keyword-taken-variables- by learned I mean read on the internet as I haven’t used them in my code. Because it seems weird. And they are ugly. I haven’t used verbatim identifiers, but have noticed a few devs that use them, some more freely than others. Are they often used? When would be a great usage scenario?- Or are they in general best avoided?
I’m under the impression that they are best avoided,- when possible that is. But that there might be some exceptions (and I would love to know more about these exceptions).
Comments
Verbatim identifiers come really handy when u are working with razor view engine with ASP.NET MVC and try to specify HTML attributes with HTML form helpers by using anonymous types. Sample: @Html.TextBox("foo", @Model.Foo, new { @class = "foo" })
The only time I've used this is in ASP.NET MVC. If you want to set a css class on an element when you are using Html.EditorFor (or something similar) you can use it to do that. See here. Otherwise, I've never used it.
Well, I am taking the risk of sounding either ignorant or arrogant: I simply didn't know they existed in C#. Now let me explain: In my 12 years of working with C# (is it really that long? WOW) I didn't come across a situation where I had the desire of being able to use verbatim identifiers. So, it is safe to say: I had no need for them, which explains why I didn't give a damn about their existence until now. no matter how hard I think about them, I still cannot come up with a proper use for them in my daily work writing mostly LOB applications. once again, my 2 cent.
The only time I've ever found use for them is when passing in an anonymous object for HTML attributes in a Razor views and I want to specify a CSS class name; i.e. {@class="awesomeSauce"}
I like to use them in extension methods, e.g.:
public static string ToString(this string @this, string regexPattern)
{
return Regex.IsMatch(@this, regexPattern)
? Regex.Match(@this, regexPattern).Value
: null;
}
I think it's a bad practice - and if you have to go back through legacy code and add a @ to the variable - then you might as well use resharper to just change the variable name. I see this in database programming as well (allowing the escaping and using of reserved words). I just think it is confusing and hard to maintain. My vote is NO, NO, NO. :)
Are more useful scenario is adding a css class to a hyperlink using the ActionLink HtmlHelper in ASP.NET MVC. Since "class" is a reserved keyword, you wouldn't normally be able to use it while instantiating a dictionary. "@" to the rescue!
Html.ActionLink("Some Text", "SomeAction", new { @class = "class-name" })
Read this post the other day and then stumbled upon something I wrote a while back using a verbatim identifier. LoadFromConfigOrDefault A pragmatic approach: When naming an identifier, choose the best possible name. If it is a keyword, compare the disadvantage of prefixing it with @ to choosing the second best possible name.
But Class with an uppercase C won't clash with the keyword. But it could possibly cause a problem with VB.NET, which is case insensitive. That brings to mind using class names as property names which is common (I do this without a second thought) but surely confusing for newbies, example: public class Enrollment { public Student Student{get;set;} public Class Class{get;set;} }
Last modified on 2012-10-30