如何在静态类中重写 ToString?

发布于 2024-07-09 14:46:10 字数 407 浏览 7 评论 0原文

我有一个公共静态类,我想在其中有一个 ToString() 方法。

我已将其定义为 public static string ToString(),但收到以下警告:

“Class.ToString()”隐藏继承成员“object.ToString()”。 要使当前成员覆盖该实现,请添加 override 关键字。 否则添加新关键字。

如果我添加 override 关键字,则会收到此错误:

静态成员“Class.ToString()”不能标记为重写、虚拟或抽象

如何消除该警告并让我的静态类具有 ToString() 方法。

谢谢你,
基思

I have a public static class in which I would like to have a ToString() method.

I have defined it as public static string ToString(), but get the following warning:

'Class.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

If I add the override keyword I get this error instead:

A static member 'Class.ToString()' cannot be marked as override, virtual, or abstract

How do I get rid of that warning and let my static class have the ToString() method.

Thank you,
Keith

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

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

发布评论

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

评论(3

久隐师 2024-07-16 14:46:10

是的,使用“new”修饰符将有效地消除编译器警告,但您显式隐藏具有静态方法的实例方法。 (这与重写方法不同。)通常,除非有充分的理由,否则您不想隐藏实例方法,并且您确实不应该使用静态方法隐藏它,因为这确实会改变调用的行为语义。 .NET 中的每个对象都有一个名为 ToString() 的实例方法,该方法具有开发人员期望的特定行为; 通过使用新的静态方法隐藏该行为,您正在改变这种期望,这可能会导致很多混乱。

你“串线”是什么? 静态类通常不保存内部状态,因此实际上不应该提供任何内部数据作为 ToString() 调用的逻辑输出。 您可能需要重新考虑您的类设计或提供不同的方法名称,以更清楚地指示该方法的用途,而不隐藏实例 ToString()。

Yes, using the "new" modifier will effectively silence the compiler warning but you are explicitly hiding an instance method with a static method. (This is different than overriding the method.) Typically you don't want to hide an instance method except with very good reasons and you really shouldn't hide it with a static method as that really changes the behavior semantics of the call. Every object in .NET has an instance method named ToString() that has specific behavior that developers expect; by hiding that behavior with a new static method you are changing that expectation which can lead to a lot of confusion.

What are you "to stringing"? Static classes typically don't hold internal state so there really shouldn't be any internal data to provide as the logical output of a ToString() call. You may want to rethink your class design or provide a different method name that more clearly indicates the purpose of the method without hiding the instance ToString().

⊕婉儿 2024-07-16 14:46:10

在静态类中,您不能重写 ToString。 .ToString 是一个实例方法,根据定义静态类只能有静态成员。

另外为什么要重写 .ToString() ? 无法获取该类的实例,因此无法调用该函数。

注意:使用新语法不会覆盖.ToString。 它将创建一个与 Object.ToString() 方法完全无关的新成员。

In a static class you cannot override ToString. .ToString is an instance method and by definition a static class can only have static members.

Also why would you want to override .ToString()? There is no way to get an instance of the class and hence no way to call the function.

Note: Using the new syntax will not override .ToString. It will create a new member that is completely unrelated to the Object.ToString() method.

天赋异禀 2024-07-16 14:46:10

好吧,在提出问题时,我找到了答案:

新的修饰符:

http://msdn.microsoft。 com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier

现在的方法是:

公共新静态字符串ToString()

谢谢,
基思

Ok, so in asking the question, I found an answer:

The new Modifier:

http://msdn.microsoft.com/en-us/library/51y09td4(VS.71).aspx#vclrfnew_newmodifier

here is the method now:

public new static string ToString()

Thank you,
Keith

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