Microsoft文档中的C#String.Length

Dej*_*jan 0 c# string string-length

Microsoft文档声明此代码将返回7个字符

Length属性返回此实例中Char对象的数量,而不是Unicode字符数.

string characters = "abc\u0000def";
Console.WriteLine(characters.Length);    // Displays 7
Run Code Online (Sandbox Code Playgroud)

我将需要一个函数来返回结果12,因为有12个不同的字符.我可以使用哪种功能?

Cee*_* it 10

您必须阻止编译器对文字的解释.这可以使用@前缀完成,如下所示:

var characters = @"abc\u0000def";
Run Code Online (Sandbox Code Playgroud)

然后Length,此字符串的属性将返回12,但字符串中将不再存在实际的unicode字符.