c#中@标志的作用
前言:今日遇到了一个问题:怎么将含有单引号和双引号的复杂sql放入,string.farmat(@"");? 中?? ?这其中遇到了一个关键字@,? 我使用@的目的是让sql语句可以换行写,但是遇到了里面含有双引号的问题。 突然忘了怎么该把双引号给转义了,使用反斜杠:""? 不好用,因为有@关键字,? ?在@关键字中想转义双引号,需要使用一对才行,后台百度查询了其他道友的文章,记录下做分享吧: ? 重点:在字符串前加@,字符串中的转义字符串将不再转义。例外:""仍将转义为",{{和}}仍将转义为{和}。在同时使用字符串内插和逐字字符串时,$要在@的前面 ? 道友文章:https://www.cnblogs.com/zhaochenxi/p/10570482.html ? 参考微软官方文档-特殊字符@,地址?https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/verbatim 1、在变量名前加@,可以告诉编译器,@后的就是变量名。主要用于变量名和C#关键字重复时使用。 string[] @for = { "John","James","Joan","Jamie" }; for (int ctr = 0; ctr < @for.Length; ctr++) { Console.WriteLine($"Here is your gift,{@for[ctr]}!"); } // The example displays the following output: // Here is your gift,John! // Here is your gift,James! // Here is your gift,Joan! // Here is your gift,Jamie! 2、在字符串前加@,字符串中的转义字符串将不再转义。例外:""仍将转义为",{{和}}仍将转义为{和}。在同时使用字符串内插和逐字字符串时,$要在@的前面 string filename1 = @"c:documentsfilesu0066.txt"; string filename2 = "c:documentsfilesu0066.txt"; Console.WriteLine(filename1); Console.WriteLine(filename2); // The example displays the following output: // c:documentsfilesu0066.txt // c:documentsfilesu0066.txt 3、类似于第一条,用于在命名冲突时区分两个特性名。特性Attribute自定义的类型名称在起名时应以Attribute结尾,例如InfoAttribute,之后我们可以用InfoAttribute或Info来引用它。但是如果我们定义了两个自定义特性,分别命名Info和InfoAttribute,则在使用Info这个名字时,编译器就不知道是哪个了。这时,如果想用Info,就用@Info,想用InfoAttribute,就把名字写全。 using System; [AttributeUsage(AttributeTargets.Class)] public class Info : Attribute { private string information; public Info(string info) { information = info; } } [AttributeUsage(AttributeTargets.Method)] public class InfoAttribute : Attribute { private string information; public InfoAttribute(string info) { information = info; } } [Info("A simple executable.")] // Generates compiler error CS1614. Ambiguous Info and InfoAttribute. // Prepend '@' to select 'Info'. Specify the full name 'InfoAttribute' to select it. public class Example { [InfoAttribute("The entry point.")] public static void Main() { } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .net – SqlDataSource与ObjectDataSource
- asp.net-mvc – 如何管理服务层中的事务?
- asp.net – 有没有理由不将pdb文件部署到生产Web服务器?
- asp.net-mvc – AngularJS无法在发布模式下工作(缩小)
- 如何使用ASP.NET授权允许访问.css文件?
- ASP.NET -- WebForm -- Cookie的使用
- asp.net-mvc – 为什么Asp.net MVC4不能使用SQL Server会话
- asp.net-mvc – 如何使ASP.NET MVC mini分析器与Linq 2 SQL
- asp.net Mvc4 使用ajax结合分页插件实现无刷新分页
- asp.net – Windows Azure一般问题
- ASP.NET CompareValidator问题
- ASP.Net Web应用程序尝试使用Impersonation和Del
- asp.net-mvc – VS2008中的“publish …”动作是
- 使用ASP.NET MVC的jQuery表单验证
- asp.net-mvc – MVC DropDownListFor和StringLen
- asp.net – 消息处理程序和Http模块?
- asp.net-mvc – 如何为特定的Controller / Actio
- asp.net-mvc-3 – ASP.NET MVC 3在web.config中的
- ASP.NET和底层连接已关闭:无法为SSL / TLS安全通
- asp.net – 总是显示FooterTemplate,甚至没有数据