如何在静态类中重写ToString?

时间:2023-01-15 18:55:38

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

我有一个公共静态类,我希望有一个ToString()方法。

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

我已将其定义为公共静态字符串ToString(),但得到以下警告:

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

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

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

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

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

静态成员'Class.ToString()'不能标记为override,virtual或abstract

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

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

Thank you,
Keith

谢谢,基思

3 个解决方案

#1


12  

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.

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

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().

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

#2


3  

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

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

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.

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

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

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

#3


2  

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()

public new static string ToString()

Thank you, Keith

谢谢,基思

#1


12  

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.

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

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().

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

#2


3  

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

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

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.

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

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

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

#3


2  

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()

public new static string ToString()

Thank you, Keith

谢谢,基思