• $ 和 @ 的用法


    $ - 字符串内插

    $ 特殊字符将字符串文本标识为内插字符串 。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。 从 C# 6 开始可以使用此功能。

    若要将字符串标识为内插字符串,可在该字符串前面加上 $ 符号。 字符串文本开头的 $ 和 " 之间不能有任何空格。

    举例:

    string name = "Mark";
    var date = DateTime.Now;
    
    // Composite formatting:
    Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
    // String interpolation:
    Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
    // Both calls produce the same output that is similar to:
    // Hello, Mark! Today is Wednesday, it's 19:40 now.

    @ - 逐字字符串标识符

    @ 特殊字符用作原义标识符。 它具有以下用途:

    1、使 C# 关键字用作标识符。 @ 字符可作为代码元素的前缀,编译器将把此代码元素解释为标识符而非 C# 关键字。 下面的示例使用 @ 字符定义其在 for 循环中使用的名为 for 的标识符。

    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、指示将原义解释字符串。 @ 字符在此实例中定义原义标识符 。 简单转义序列(如代表反斜杠的 "\")、十六进制转义序列(如代表大写字母 A 的 "x0041")和 Unicode 转义序列(如代表大写字母 A 的 "u0041")都将按字面解释。 只有引号转义序列 ("") 不会按字面解释;因为它生成一个双引号。 此外,如果是逐字内插字符串,大括号转义序列({{ 和 }})不按字面解释;它们会生成单个大括号字符。 下面的示例分别使用常规字符串和原义字符串定义两个相同的文件路径。 这是原义字符串的较常见用法之一。

    string filename1 = @"c:documentsfilesu0066.txt";
    string filename2 = "c:\documents\files\u0066.txt";
    
    Console.WriteLine(filename1);
    Console.WriteLine(filename2);
    // The example displays the following output:
    //     c:documentsfilesu0066.txt
    //     c:documentsfilesu0066.txt

    3、使编译器在命名冲突的情况下区分两种属性。 属性是派生自 Attribute 的类。 其类型名称通常包含后缀 Attribute,但编译器不会强制进行此转换。 随后可在代码中按其完整类型名称(例如 [InfoAttribute])或短名称(例如 [Info])引用此属性。 但是,如果两个短名称相同,并且一个类型名称包含 Attribute 后缀而另一类型名称不包含,则会出现命名冲突。 例如,由于编译器无法确定将 Info 还是 InfoAttribute 属性应用于 Example 类,因此下面的代码无法编译。

    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()
       {
       }
    }

    【转载】:https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/tokens/

  • 相关阅读:
    解决方案-BI:百科
    un-Error-ASP.NET:“/tbm6”应用程序中的服务器错误。
    JS-jQuery-EasyUI-Layout-Tabs:Tabs 标签页/选项卡
    JS-jQuery-EasyUI-Layout:Layout 布局
    jQuery-EasyUI-CSS:Icon 图标
    jQuery-EasyUI:Layout
    JS-jQuery-EasyUI:CSS
    JS-jQuery-EasyUI :目录
    ORM:百科
    ORM- IBatisNet:百科
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/13661209.html
Copyright © 2020-2023  润新知