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

后端 未结 9 899
小蘑菇
小蘑菇 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:59

    In C# the at (@) character is used to denote literals that explicitly do not adhere to the relevant rules in the language spec.

    Specifically, it can be used for variable names that clash with reserved keywords (e.g. you can't use params but you can use @params instead, same with out/ref/any other keyword in the language specification). Additionally it can be used for unescaped string literals; this is particularly relevant with path constants, e.g. instead of path = "c:\\temp\\somefile.txt" you can write path = @"c:\temp\somefile.txt". It's also really useful for regular expressions.

提交回复
热议问题