C#@的功能总结

来源:互联网 发布:mysql数据库管理 编辑:程序博客网 时间:2024/06/03 09:42

C#中@的用法

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

C#代码 
  1. string path = @"C:\Windows\"// 如果不加 @,编译会提示无法识别的转义序列
  2. // 如果不加 @,可以写成如下
  3. string path2 = "C:\\Windows\\";
[c#] view plain copy
  1. string path = @"C:\Windows\"// 如果不加 @,编译会提示无法识别的转义序列  
  2. // 如果不加 @,可以写成如下  
  3. string path2 = "C:\\Windows\\";  

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

C#代码 
  1. string str = @"aaa=""bbb""";
  2. // 不加 @,可以写成
  3. string str2 = "aaa=\"bbb\"";
[c#] view plain copy
  1. string str = @"aaa=""bbb""";  
  2. // 不加 @,可以写成  
  3. string str2 = "aaa=\"bbb\"";  

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

C#代码 
  1. string insert = @"
  2. insert into Users
  3. (
  4. UserID,
  5. Username,
  6. Email
  7. ) values
  8. (
  9. @UserID,
  10. @Username,
  11. @Email
  12. )";
[c#] view plain copy
  1. string insert = @"  
  2. insert into Users  
  3. (  
  4. UserID,  
  5. Username,  
  6. Email  
  7. ) values  
  8. (  
  9. @UserID,  
  10. @Username,  
  11. @Email  
  12. )";  

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

C#代码 
  1. string @operator = "+";
  2. string @class = "分类一";
  3. Console.WriteLine(@operator);
  4. Console.WriteLine(@class);
[c#] view plain copy
  1. string @operator = "+";  
  2. string @class = "分类一";  
  3. Console.WriteLine(@operator);  
  4. Console.WriteLine(@class);  


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

C#代码 
  1. string delete = "delete from Categery where CategoryID=@CategoryID";
  2. SqlConnection connection = new SqlConnection("connectionString");
  3. SqlCommand command = new SqlCommand(delete, connection);
  4. command.Parameters.Add("@CategoryID", SqlDbType.BigInt);
原创粉丝点击