StringBuilder Append 和 AppendLine 对于 Enum 给出不同的结果

发布于 2024-10-15 18:01:11 字数 1142 浏览 0 评论 0原文

使用字符串生成器时,当使用枚举作为输入时,我从追加和追加行函数中获得预期结果,但是当枚举被装箱时,追加行和追加函数给出不同的结果。

谁能告诉我这背后可能是什么?

代码输出:

Append Enum: 1
Append Enum To String: One
Append Line Enum: 1
Append Line Enum To String: One
Append Object: One
Append Object To String: One
Append Line Object: 1
Append Line Object To String: One

代码:

Public Enum eTest
    One = 1
    Two = 2
End Enum

Sub Main()
    Dim sb As New System.Text.StringBuilder()
    Dim x = eTest.One
    sb.Append("Append Enum: ").Append(x).AppendLine()
    sb.Append("Append Enum To String: ").Append(x.ToString()).AppendLine()
    sb.Append("Append Line Enum: ").AppendLine(x)
    sb.Append("Append Line Enum To String: ").AppendLine(x.ToString())

    Dim o As Object = x
    sb.Append("Append Object: ").Append(o).AppendLine()
    sb.Append("Append Object To String: ").Append(o.ToString()).AppendLine()
    sb.Append("Append Line Object: ").AppendLine(o)
    sb.Append("Append Line Object To String: ").AppendLine(o.ToString())

    Console.WriteLine(sb.ToString())


    '===============================
    Console.ReadKey()
End Sub

When using a string builder, I get expected results from the append and append line functions when using an enum is the input, but when the enum is boxed the append line and append function give differing results.

Can anyone tell me what may be behind this?

Code output:

Append Enum: 1
Append Enum To String: One
Append Line Enum: 1
Append Line Enum To String: One
Append Object: One
Append Object To String: One
Append Line Object: 1
Append Line Object To String: One

Code:

Public Enum eTest
    One = 1
    Two = 2
End Enum

Sub Main()
    Dim sb As New System.Text.StringBuilder()
    Dim x = eTest.One
    sb.Append("Append Enum: ").Append(x).AppendLine()
    sb.Append("Append Enum To String: ").Append(x.ToString()).AppendLine()
    sb.Append("Append Line Enum: ").AppendLine(x)
    sb.Append("Append Line Enum To String: ").AppendLine(x.ToString())

    Dim o As Object = x
    sb.Append("Append Object: ").Append(o).AppendLine()
    sb.Append("Append Object To String: ").Append(o.ToString()).AppendLine()
    sb.Append("Append Line Object: ").AppendLine(o)
    sb.Append("Append Line Object To String: ").AppendLine(o.ToString())

    Console.WriteLine(sb.ToString())


    '===============================
    Console.ReadKey()
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2024-10-22 18:01:11

此行

sb.Append("Append Object: ").Append(o).AppendLine()

导致将 o.ToString() 附加到 StringBuilder 的实例,这就是区别。当然,该调用只是传递到枚举的装箱实例,这就是为什么您会看到此调用的 One

这是因为您正在调用重载 StringBuilder.Append(object)只会在传入的对象实例上调用 o.ToString() 。文档中明确说明了这一点:

Append 方法调用 Object.ToString 方法来获取 value 的字符串表示形式。如果 valuenull,则不会对 StringBuilder 对象进行任何更改。

另一方面,当您调用

sb.Append("Append Enum: ").Append(x).AppendLine()

其中 x 是枚举实例时,x 将隐式转换为 int 实例,这将调用重载StringBuilder.Append(int)

请注意,这是特定于 VB.NET 的。在选择调用哪个 StringBuilder.Append 重载时,C# 不会将 x 隐式转换为 int;它将调用StringBuilder.Append(object)

This line

sb.Append("Append Object: ").Append(o).AppendLine()

causes o.ToString() to be appended to the instance of StringBuilder and that is the difference. Of course, that call just passes through to the boxed instance of your enum and this is why you see One for this invocation.

This is because you are invoking the overload StringBuilder.Append(object) which will just invoke o.ToString() on the passed in instance of object. This is explicitly in the documentation:

The Append method calls the Object.ToString method to get the string representation of value. If value is null, no changes are made to the StringBuilder object.

On the other hand, when you call

sb.Append("Append Enum: ").Append(x).AppendLine()

where x is an instance of an enum, x will be implicitly converted to an instance of int which will call the overload StringBuilder.Append(int).

Note that this is specific to VB.NET. C# will not implicitly convert x to an int when choosing which overload of StringBuilder.Append to invoke; it will invoke StringBuilder.Append(object).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文