错误:can';t将虚拟/抽象成员声明为私有成员
本文关键字:成员 抽象 声明 虚拟 can 错误 | 更新日期: 2023-09-27 18:08:22
可能重复:
为什么私有虚拟方法在C#中是非法的?
我在C#中有以下代码,Visual Studio在Derived类中抱怨我不能声明虚拟/抽象成员私有。。但我不是。。有人有什么想法吗?感谢
public class Base
{
private const string Name= "Name1";
protected virtual string Member1
{
get{
return Name;
}
}
}
public class Derived: Base
{
private const string Name= "Name2";
protected override string Member1
{
get{
return Name;
}
}
}
无法复制,修复了"class"的情况并提供了方法体:
class Base
{
protected virtual string Member1() { return null; }
}
class Derived : Base
{
protected override string Member1() { return null; }
}
此编译没有任何警告。
如果您试图将字段声明为虚拟字段,则会得到:
Test.cs(11,30(:错误CS0106:修饰符"virtual"对此项无效
Test.cs(17,31(:错误CS0106:修饰符"override"对此项无效
虚拟方法必须有一个主体:
public class Base
{
protected virtual string Member1()
{
return "";
}
}
public class Derived: Base
{
protected override string Member1()
{
return "this is the ovveride";
}
}