C#中@作用是什么呢?
在很多其他语言中,@一般没意义,在c#中,它的意有以下四点:
- 1:对字符串中的需要转义的不用转义:
string path = @"D:\Microsoft VS Code\Code.exe"; //如果不加@那么编译的时候就会提示无法识别转义序列
//如果不加@ path 写法如下
string path_1="D:\\Microsoft VS Code\\Code.exe"";//就需要\进行转义
- 2:加在字符串前面,字符串中的" 要用" "表示(不需要转义但是需要确定结尾)
//如果加@的话
string s=@"aaa=\b==""";
//如果没有@的话 就是相当于转义了
string s="aaa=\b==\"";
- 3:在关键字做变量的时候 加@就不是关键字了 而是一个变量了
string @string = "test";
@string = "我现在不是一个关键字了哦";
Console.WriteLine(@string);
Console.ReadKey();
-以下我就不懂了
- 4:加在字符串前面,换行空格都保存着,方便阅读代码
string insert = @"
insert into Users
(
UserID,
Username,
Email
) values
(
@UserID,
@Username,
@Email
)";
- 5: 作为sql语句里的一个“标签”,声明此处需要插入一个参数
string delete="delete from Categery where CategoryId=@CategoryID";
sqlConnection connection=new SqlConnection("connectionString");
SqlCommand command =new SqlCommand(delete,connection);
command.Parametes.Add("@CategoryID",SqlDbType.BigInt);