当前位置: 移动技术网 > IT编程>开发语言>c# > 解析C#中@符号的几种使用方法详解

解析C#中@符号的几种使用方法详解

2019年07月18日  | 移动技术网IT编程  | 我要评论
1.限定字符串用 @ 符号加在字符串前面表示其中的转义字符“不”被处理。 如果我们写一个文件的路径,例如"d:/文本文件"路径下的text.txt文件,不加@符号的话写法如
1.限定字符串
用 @ 符号加在字符串前面表示其中的转义字符“不”被处理。
如果我们写一个文件的路径,例如"d:/文本文件"路径下的text.txt文件,不加@符号的话写法如下:
stringfilename="d://文本文件//text.txt";
如果使用@符号就会比较简单:
stringfilename=@"d:/文本文件/text.txt";

2.让字符串跨行
有时候一个字符串写在一行中会很长(比如sql语句),不使用@符号,一种写法是这样的:
复制代码 代码如下:

string strsql="select * from humanresources.employee as e"  
+"inner joinperson.contact as c"   3.+"on e.contactid=c.contactid"   4.+"orderby c.lastname";  

加上@符号后就可以直接换行了:
复制代码 代码如下:

string strsql=@"select * from humanresources.employee as e inner join person.contact as c on e.contactid=c.contactid orderbyc.lastname";  

3.在标识符中的用法
c#是不允许关键字作为标识符(类名、变量名、方法名、表空间名等)使用的,但如果加上@之后就可以了,例如:
复制代码 代码如下:

namespace @namespace  
{  
  class @class   4.     {  
        public static void @static(int @int)   6.         {  
            if (@int > 0)   8.             {  
                 system.console.writeline("positive integer");   10.             }  
            else if (@int == 0)   12.             {  
                 system.console.writeline("zero");   14.             }  
            else   16.             {  
                 system.console.writeline("negative integer");   18.             }  
         }  
     }  
}  

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网