C# 函数的“静态新”修饰符有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/661246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the point of "static new" modifier for a function?
提问by Morgan Cheng
Today, I found something in legacy code. It has "static new" for one function. It looks like this.
今天,我在遗留代码中发现了一些东西。它有一个功能的“静态新”。它看起来像这样。
class Foo
{
public static void Do()
{
Console.WriteLine("Foo.Do");
}
}
class Bar: Foo
{
public static new void Do()
{
Console.WriteLine("Bar.Do");
}
}
I don't understand the static newmodifier for the Domethod in class Bar. In C#, static method can only be invoked with class name instead of object name. So, I don't think there is any difference between having the "new" and not.
我不明白类Bar 中Do方法的静态 new修饰符。在 C# 中,静态方法只能用类名而不是对象名来调用。因此,我认为拥有“新”和“没有”之间没有任何区别。
Generally, if some syntax is unnecessary, C# just treat it is error. Anybody has any idea about why C# allows such syntax?
通常,如果某些语法是不必要的,C# 只是将其视为错误。任何人都知道为什么 C# 允许这种语法?
采纳答案by Rasmus Faber
If you remove the newfrom your code you get:
如果您从代码中删除新的,您会得到:
warning CS0108: 'Bar.Do()' hides inherited member 'Foo.Do()'. Use the new keyword if hiding was intended.
警告 CS0108:“Bar.Do()”隐藏继承的成员“Foo.Do()”。如果打算隐藏,请使用 new 关键字。
The C# compiler just warns you that you might be doing something you did not intend and asks you to insert the newkeyword to confirm that you know what you are doing. Besides suppressing the warning, it has no other effects.
C# 编译器只是警告您可能正在做一些您不打算做的事情,并要求您插入new关键字以确认您知道自己在做什么。除了抑制警告之外,它没有其他效果。
回答by Andreas Niedermair
have a look at this
看看这个
public class BaseC { public static int x = 55; public static int y = 22; } public class DerivedC : BaseC { // Hide field 'x'. new public static int x = 100; static void Main() { // Display the new value of x: Console.WriteLine(x); // Display the hidden value of x: Console.WriteLine(BaseC.x); // Display the unhidden member y: Console.WriteLine(y); } } /* Output: 100 55 22 */
public class BaseC { public static int x = 55; public static int y = 22; } public class DerivedC : BaseC { // Hide field 'x'. new public static int x = 100; static void Main() { // Display the new value of x: Console.WriteLine(x); // Display the hidden value of x: Console.WriteLine(BaseC.x); // Display the unhidden member y: Console.WriteLine(y); } } /* Output: 100 55 22 */
Your example, to make it clear, should be
你的例子,为了清楚起见,应该是
public class Foo
{
public static void Do() {}
}
public class Bar :Foo
{
public new static void Do() {}
}
回答by Cerebrus
In your example, the second class Bar doesn't seem to inherit from Foo. This seems to be a typo because it doesn't makes sense otherwise.
在您的示例中,第二个类 Bar 似乎不是从 Foo 继承的。这似乎是一个错字,因为否则它没有意义。
Assuming it to be a typo, the "new" modifier explicitly hides the base class version of the function Do()
. This means that the derived version of the Do()
method effectively replaces the base class version.
假设它是一个错字,“new”修饰符显式隐藏了函数的基类版本Do()
。这意味着该方法的派生版本Do()
有效地替换了基类版本。
Using "new" here documents the fact that the inherited method is intended as a replacement for the base class version of Do()
.
在这里使用“new”记录了这样一个事实,即继承的方法旨在替代Do()
.
For more information, see new Modifier (C#)
有关更多信息,请参阅新修饰符 (C#)
回答by eglasius
That applies only for external callers. Remember that you can call a static method of the base class, so something like this is valid:
这仅适用于外部呼叫者。请记住,您可以调用基类的静态方法,因此以下内容是有效的:
class Foo
{
public static void Do() { Console.WriteLine("Foo.Do"); }
}
class Bar : Foo // :Foo added
{
public static void Something()
{
Do();
}
}
This is why the warning tells you to put the new, you want to avoid any confusion when doing this:
这就是为什么警告告诉您放置新的,您希望在执行此操作时避免任何混淆:
class Foo
{
public static void Do() { Console.WriteLine("Foo.Do"); }
}
class Bar : Foo // :Foo added
{
public static void Something()
{
Do();
}
public static new void Do() { Console.WriteLine("Bar.Do"); }
}