自动.ToString()?

发布于 2024-09-24 22:08:51 字数 479 浏览 9 评论 0原文

我有一个这样的方法: void m1(string str) 并有一个这样的类:

public class MyClass
{
    public bool b1 { set; get; }

    //and other properties
}

现在为什么下面的代码不会导致编译错误?

IClass2 _class2 = new Class2();
MyClass c1 = new MyClass();
_class2.m1("abcdef" + c1);

当我调试它时,我意识到c1.ToString()已被传递给m1。为什么会发生这种自动 .ToString() ?我唯一能说的是m1已经在IClass2接口中定义,并由Class2实现。

I have a method like this: void m1(string str) and have a class like this:

public class MyClass
{
    public bool b1 { set; get; }

    //and other properties
}

Now why following code does not cause compile error?

IClass2 _class2 = new Class2();
MyClass c1 = new MyClass();
_class2.m1("abcdef" + c1);

When I debug it, I realized that c1.ToString() has been passed to m1. Why this automatic .ToString() has been occurred? The only thing I could say is that m1 has been defined in IClass2 interface and has been implemented by Class2.

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

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

发布评论

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

评论(2

扛起拖把扫天下 2024-10-01 22:08:51

这遵循 C# 语言规范中有关字符串连接的规则。请参阅 C# 4 规范的第 7.8.4 节(加法运算符)

字符串连接:

字符串运算符 +(字符串 x, 字符串 y);
字符串运算符 +(字符串 x, 对象 y);
字符串运算符 +(对象 x, 字符串 y);

二元+ 运算符的这些重载执行字符串连接。如果字符串连接的操作数为null,则替换为空字符串。否则,任何非字符串参数都会通过调用从类型对象继承的虚拟 ToString 方法转换为其字符串表示形式。如果 ToString 返回 null,则替换为空字符串。

如果您没想到会发生这种情况,请问您期望 "abcdef" + c1 表达式的结果是什么?

请注意,m1IClass2Class2 与此处发生的情况无关 - 只有串联表达式才是真正相关的:这就是触发调用的原因到 ToString(),无论该字符串稍后发生什么情况。

This follows the rules of the C# language specification around string concatenation. See section 7.8.4 of the C# 4 spec (the addition operator)

String concatenation:

string operator +(string x, string y);
string operator +(string x, object y);
string operator +(object x, string y);

These overloads of the binary + operator perform string concatenation. If an operand of string concatenation isnull, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object. If ToString returns null, an empty string is substituted.

If you didn't expect that to happen, may I ask what you expected the result of the "abcdef" + c1 expression to be?

Note that m1, IClass2 and Class2 are irrelevant to what's happening here - it's only the concatenation expression which is really relevant: that's what's triggering the call to ToString(), regardless of what's later happening to that string.

忆离笙 2024-10-01 22:08:51

编译器将 "abcdef" + c1 转换为 string.Concat(object,object) 调用。这反过来会对每个参数调用 .ToString() 并将它们连接起来。以下是来自 Reflector 的代码:

public static string Concat(object arg0, object arg1)
{
   if(arg0 == null) arg0 = Empty;
   if(arg1 == null) arg1 = Empty;

   return (arg0.ToString() + arg1.ToString());
}

请注意,最后一行涉及对 string.Concat(string, string) 的调用,其中发生真正的连接。

The compiler turns "abcdef" + c1 into a string.Concat(object,object) call. This in turn will call .ToString() on each of the arguments and concatenate them. Here's the code from reflector:

public static string Concat(object arg0, object arg1)
{
   if(arg0 == null) arg0 = Empty;
   if(arg1 == null) arg1 = Empty;

   return (arg0.ToString() + arg1.ToString());
}

Note that the last line involves a call to string.Concat(string, string) where the real concatenation happens.

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