开发者

Error: can't declare a virtual/ abstract member private [duplicate]

This question already has answers here: Closed 11 years ago. 开发者_高级运维

Possible Duplicate:

Why are private virtual methods illegal in C#?

I have the following code in C#, and Visual Studio is complaining in the Derived class that i cant declare a virtual/ abstract member private.. but i am not .. so does anyone have some ideas? Thanks

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;
     }
 }   
}


Unable to reproduce, having fixed the case of "Class" and provided method bodies:

class Base
{    
    protected virtual string Member1() { return null; }    
}

class Derived : Base
{
    protected override string Member1() { return null; }   
}

This compiles with no warnings.

If you were trying to declare fields as virtual, you'd get:

Test.cs(11,30): error CS0106: The modifier 'virtual' is not valid for this item
Test.cs(17,31): error CS0106: The modifier 'override' is not valid for this item


virtual method has to have a body:

public class  Base
{

    protected virtual string Member1()
    {
        return "";
    }

}

public class Derived: Base
{
    protected override string Member1()
    {
        return "this is the ovveride";
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜