C#中变量/标识符前面的@是什么?

What is @ in front of a variable / identifier in C#? [duplicate]
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我认为我的问题与此不同: C# 中字符串前面的@ 是什么?

我在 VB.net 中工作,所以这在 C# 中可能是一些简单的事情,但我不知道这一点.

我得到以下代码,其中字符串变量中有 10 个 XML.在下面的代码片段中调用 LoadXml 方法时,请建议在 claimList 字符串变量前面需要什么 @ 符号:

private void UploadNewClaims(PMAUser grumble, string companyAbbreviation, string claimsList)
{
    var claimDoc = new System.Xml.XmlDocument();
    claimDoc.LoadXml(@claimsList);
解决过程1

在这种情况下,它完全没有必要,但它允许您在 C# 中使用任何关键字作为标识符.它根本不会改变标识符的含义,或者它的使用方式——它只是告诉编译器你希望以下字符被识别为关键字.

例如:

string @int = "hello";
var @void = @int;

使用它作为标识符claimsList表明编写它的人不理解它.标识符用于字符串变量的事实在这里完全无关.

就我个人而言,我几乎只使用过扩展方法的功能,我知道我会调用第一个参数@this:

public static void Foo(this Bar @this)
{
    return @this.Baz() * 2;
}

I think my question is different from this: What's the @ in front of a string in C#?

I work in VB.net, so this may be some simple thing in C#, but I am not aware of this.

I got the following code where I have 10 XML inside a string variable. Please advice what @ symbol is needed in front of the claimsList string variable when calling LoadXml method in the code snippet below:

private void UploadNewClaims(PMAUser grumble, string companyAbbreviation, string claimsList)
{
    var claimDoc = new System.Xml.XmlDocument();
    claimDoc.LoadXml(@claimsList);
Solutions1

In this case it's completely unnecessary, but it allows you to use any keyword as an identifier in C#. It doesn't change the meaning of the identifier at all, or how it's used - it only tells the compiler that you don't want the following characters to be recognized as a keyword.

For example:

string @int = "hello";
var @void = @int;

Using it for an identifier of claimsList suggests that whoever wrote it doesn't understand it. The fact that the identifier is for a string variable is entirely irrelevant here.

Personally I've pretty much only ever used the feature for extension methods, where I have been known to call the first parameter @this:

public static void Foo(this Bar @this)
{
    return @this.Baz() * 2;
}
转载于:https://stackoverflow.com/questions/15239479/what-is-in-front-of-a-variable-identifier-in-c

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入