加深C# 中字符串前加@符号理解以及使用~~

来源:互联网 发布:linux服务器管理系统 编辑:程序博客网 时间:2024/06/05 04:34

1、 用 @ 引起来的字符串以 @ 开头,并用双引号引起来。例如:

@"good morning" // a string literal
用 @ 引起来的优点在于换码序列“不”被处理,这样就可以轻松写出字符串,例如一个完全限定的文件名:

@"c:/Docs/Source/a.txt" // rather than "c://Docs//Source//a.txt"
若要在一个用 @ 引起来的字符串中包括一个双引号,请使用两对双引号:

@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
@ 符号的另一种用法是使用碰巧成为 C# 关键字的被引用的 (/reference) 标识符。有关更多信息,请参见 2.4.2 标识符。  

2、

先看代码(以下代码使用在C#,环境ASP.NET):

    protected void Page_Load(object sender, EventArgs e)
     {
         test1(
"/a");
         test1(
@"/a");

         test2(
"/a");
         test2(
@"/a");

         test3(
"/a");
         test3(
@"/a");

         test4(
"/a");
         test4(
@"/a");
     }

    
//参数不带@ 输出不带@
    public void test1(string str)
     {
         Response.Write(
"test1:[" + str+"]
");
     }

    
//参数不带@ 输出带@
    public void test2(string str)
     {
         Response.Write(
"test1:[" + @str + "]
");
     }
    
    
//参数带@ 输出不带@
    public void test3(string @str)
     {
         Response.Write(
"test1:[" + str + "]
");
     }

    
//参数带@ 输出带@
    public void test4(string @str)
     {
         Response.Write(
"test1:[" + @str + "]
");
    }

F5执行,猜猜什么结果!!嘿嘿~~
以下公布显示结果:
test1:[ ]
test1:[/a]
test1:[ ]
test1:[/a]
test1:[ ]
test1:[/a]
test1:[ ]
test1:[/a]

o(∩_∩)o...哈哈。
可以发现无论你后来给不给字串加@符号,都不管用了,只有在字符串产生的时候加@有效果!

原创粉丝点击