我是靠谱客的博主 怕黑小松鼠,这篇文章主要介绍xml中特殊含义的字符_C#中特殊字符的用法总结,现在分享给大家,希望可以做个参考。

49062e2e5ba879e9ac703e9c9f8a4e00.png
@ - C# 参考​docs.microsoft.com $ - 字符串内插 - C# 参考​docs.microsoft.com
3f55a06ce678bae7ee9af49d431c2f20.png

$字符

$ 特殊字符将字符串文本标识为内插字符串 。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。

与使用字符串复合格式设置功能创建格式化字符串相比,字符串内插提供的语法更具可读性,且更加方便。 下面的示例使用了这两种功能生成同样的输出结果:

复制代码
1
2
3
4
5
6
7
8
9
10
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.

内插字符串的结构

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

具备内插表达式的项的结构如下所示:

{<interpolationExpression>[,<alignment>][:<formatString>]}

下面说明一下每个元素的意义:

debf80c87f9c1d1a9d9b05227d8f7bda.png

以下示例使用上述可选的格式设置组件:

复制代码
1
2
3
4
5
6
7
8
9
Console.WriteLine($"|{"Left",-7}|{"Right",7}|"); const int FieldWidthRightAligned = 20; Console.WriteLine($"{Math.PI,FieldWidthRightAligned} - default formatting of the pi number"); Console.WriteLine($"{Math.PI,FieldWidthRightAligned:F3} - display only three decimal digits of the pi number"); // Expected output is: // |Left | Right| // 3.14159265358979 - default formatting of the pi number // 3.142 - display only three decimal digits of the pi number

特殊字符

要在内插字符串生成的文本中包含大括号 "{" 或 "}",请使用两个大括号,即 "{{" 或 "}}"。 有关详细信息,请参阅转义大括号。因为冒号(“:”)在内插表达式项中具有特殊含义,为了在内插表达式中使用条件运算符,请将表达式放在括号内。

以下示例演示如何将大括号含入结果字符串中,以及如何在内插表达式中使用条件运算符:

复制代码
1
2
3
4
5
6
7
8
string name = "Horace"; int age = 34; Console.WriteLine($"He asked, "Is your name {name}?", but didn't wait for a reply :-{{"); Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old."); // Expected output is: // He asked, "Is your name Horace?", but didn't wait for a reply :-{ // Horace is 34 years old.

@字符

@字符主要用作原义标识符,它具有以下用途:

1. 使C#关键字用作标识符

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
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")都将按字面解释。 只有引号转义序列 ("") 不会按字面解释;因为它生成一个双引号。 此外,如果是逐字内插字符串,大括号转义序列({{ 和 }})不按字面解释;它们会生成单个大括号字符。 下面的示例分别使用常规字符串和原义字符串定义两个相同的文件路径。 这是原义字符串的较常见用法之一。

复制代码
1
2
3
4
5
6
7
8
9
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

下面的示例演示定义包含相同字符序列的常规字符串和原义字符串的效果。

复制代码
1
2
3
4
5
6
7
8
9
string s1 = "He said, "This is the last u0063hancex0021""; string s2 = @"He said, ""This is the last u0063hancex0021"""; Console.WriteLine(s1); Console.WriteLine(s2); // The example displays the following output: // He said, "This is the last chance!" // He said, "This is the last u0063hancex0021"

3. 使编译器在命名冲突的情况下区分两种属性

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

最后

以上就是怕黑小松鼠最近收集整理的关于xml中特殊含义的字符_C#中特殊字符的用法总结的全部内容,更多相关xml中特殊含义内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部