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

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]

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值