What's the @ in front of a string in C#?

前端 未结 9 1428
一整个雨季
一整个雨季 2020-11-22 01:04

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what\'s the difference between the following declarations:

string hello =          


        
9条回答
  •  遥遥无期
    2020-11-22 01:45

    An '@' has another meaning as well: putting it in front of a variable declaration allows you to use reserved keywords as variable names.

    For example:

    string @class = "something";
    int @object = 1;
    

    I've only found one or two legitimate uses for this. Mainly in ASP.NET MVC when you want to do something like this:

    <%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>
    

    Which would produce an HTML link like:

    Text
    

    Otherwise you would have to use 'Class', which isn't a reserved keyword but the uppercase 'C' does not follow HTML standards and just doesn't look right.

提交回复
热议问题