What's the use/meaning of the @ character in variable names in C#?

后端 未结 9 912
小蘑菇
小蘑菇 2020-11-22 01:25

I discovered that you can start your variable name with a \'@\' character in C#. In my C# project I was using a web service (I added a web reference to my project) that was

9条回答
  •  误落风尘
    2020-11-22 01:50

    Unlike Perl's sigils, an @ prefix before a variable name in C# has no meaning. If x is a variable, @x is another name for the same variable.

    > string x = "abc";
    > Object.ReferenceEquals(x, @x).Dump();
    True
    

    But the @ prefix does have a use, as you've discovered - you can use it to clarify variables names that C# would otherwise reject as illegal.

    > string string;
    Identifier expected; 'string' is a keyword
    
    > string @string;
    

提交回复
热议问题