码迷,mamicode.com
首页 > Windows程序 > 详细

C#中关于@的用法

时间:2015-10-13 17:00:19      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1. 加在字符串前面,字符串中的 \ 失去转义符的作用,直接写字符串而不需要考虑转义字符

string path = @"C:\Windows\"; // 如果不加 @,编译会提示无法识别的转义序列  
// 如果不加 @,可以写成如下  
string path2 = "C:\\Windows\\";  

2. 加在字符串前面,字符串中的 " 要用 "" 表示

string str = @"aaa=""bbb""";  
// 不加 @,可以写成  
string str2 = "aaa=\"bbb\""; 

3 加在字符串前面,换行空格都保存着,方便阅读代码

string insert = @"  
insert into Users  
(  
UserID,  
Username,  
Email  
) values  
(  
@UserID,  
@Username,  
@Email  
)";  

4 用关键字做变量时在关键字前面加@

string @operator = "+";  
string @class = "分类一";  
Console.WriteLine(@operator);  
Console.WriteLine(@class);  

5 作为sql语句里的一个“标签”,声明此处需要插入一个参数

string delete = "delete from Categery where CategoryID=@CategoryID";
SqlConnection connection = new SqlConnection("connectionString");
SqlCommand command = new SqlCommand(delete, connection);
command.Parameters.Add("@CategoryID", SqlDbType.BigInt);

 

C#中关于@的用法

标签:

原文地址:http://www.cnblogs.com/liuyudong0825/p/4874698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!