StringBuilder Append 和 AppendLine 对于 Enum 给出不同的结果
使用字符串生成器时,当使用枚举作为输入时,我从追加和追加行函数中获得预期结果,但是当枚举被装箱时,追加行和追加函数给出不同的结果。
谁能告诉我这背后可能是什么?
代码输出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行
导致将
o.ToString()
附加到StringBuilder
的实例,这就是区别。当然,该调用只是传递到枚举的装箱实例,这就是为什么您会看到此调用的One
。这是因为您正在调用重载
StringBuilder.Append(object)
只会在传入的对象实例上调用
o.ToString()
。文档中明确说明了这一点:另一方面,当您调用
其中
x
是枚举实例时,x
将隐式转换为int
实例,这将调用重载StringBuilder.Append(int)
。请注意,这是特定于 VB.NET 的。在选择调用哪个
StringBuilder.Append
重载时,C# 不会将x
隐式转换为int
;它将调用StringBuilder.Append(object)
。This line
causes
o.ToString()
to be appended to the instance ofStringBuilder
and that is the difference. Of course, that call just passes through to the boxed instance of your enum and this is why you seeOne
for this invocation.This is because you are invoking the overload
StringBuilder.Append(object)
which will just invokeo.ToString()
on the passed in instance of object. This is explicitly in the documentation:On the other hand, when you call
where
x
is an instance of an enum,x
will be implicitly converted to an instance ofint
which will call the overloadStringBuilder.Append(int)
.Note that this is specific to VB.NET. C# will not implicitly convert
x
to anint
when choosing which overload ofStringBuilder.Append
to invoke; it will invokeStringBuilder.Append(object)
.