当前位置: 移动技术网 > IT编程>开发语言>.net > c#中@标志的作用

c#中@标志的作用

2019年03月22日  | 移动技术网IT编程  | 我要评论

最近有什么好看电影,用户名注册,东莞中医院

参考微软官方文档-特殊字符@,地址

1、在变量名前加@,可以告诉编译器,@后的就是变量名。主要用于变量名和c#关键字重复时使用。

string[] @for = { "john", "james", "joan", "jamie" };
for (int ctr = 0; ctr < @for.length; ctr++)
{
   console.writeline($"here is your gift, {@for[ctr]}!");
}
// the example displays the following output:
//     here is your gift, john!
//     here is your gift, james!
//     here is your gift, joan!
//     here is your gift, jamie!

2、在字符串前加@,字符串中的转义字符串将不再转义。例外:""仍将转义为",{{和}}仍将转义为{和}。在同时使用字符串内插和逐字字符串时,$要在@的前面

string filename1 = @"c:\documents\files\u0066.txt";
string filename2 = "c:\\documents\\files\\u0066.txt";

console.writeline(filename1);
console.writeline(filename2);
// the example displays the following output:
//     c:\documents\files\u0066.txt
//     c:\documents\files\u0066.txt

3、类似于第一条,用于在命名冲突时区分两个特性名。特性attribute自定义的类型名称在起名时应以attribute结尾,例如infoattribute,之后我们可以用infoattribute或info来引用它。但是如果我们定义了两个自定义特性,分别命名info和infoattribute,则在使用info这个名字时,编译器就不知道是哪个了。这时,如果想用info,就用@info,想用infoattribute,就把名字写全。

using system;

[attributeusage(attributetargets.class)]
public class info : attribute
{
   private string information;
   
   public info(string info)
   {
      information = info;
   }
}

[attributeusage(attributetargets.method)]
public class infoattribute : attribute
{
   private string information;
   
   public infoattribute(string info)
   {
      information = info;
   }
}

[info("a simple executable.")] // generates compiler error cs1614. ambiguous info and infoattribute. 
// prepend '@' to select 'info'. specify the full name 'infoattribute' to select it.
public class example
{
   [infoattribute("the entry point.")]
   public static void main()
   {
   }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网