No private virtual methods?
-
I tried to make a private method virtual, and got the following error: error CS0621: '***' : virtual or abstract members cannot be private Is it really possible that private methods can not be virtual in C#, or I am making some terrible mistake here? Thanks.
-
I tried to make a private method virtual, and got the following error: error CS0621: '***' : virtual or abstract members cannot be private Is it really possible that private methods can not be virtual in C#, or I am making some terrible mistake here? Thanks.
Look at the protected keyword...
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins -
I tried to make a private method virtual, and got the following error: error CS0621: '***' : virtual or abstract members cannot be private Is it really possible that private methods can not be virtual in C#, or I am making some terrible mistake here? Thanks.
Use
protected
. Private members are only accessible to that class, so how can a derivate class override them?Microsoft MVP, Visual C# My Articles
-
Use
protected
. Private members are only accessible to that class, so how can a derivate class override them?Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: Private members are only accessible to that class, so how can a derivate class override them? :wtf: What do you mean? Virtuality has nothing to do with access rights. In C++ it is perfectly legal (and useful) to override a virtual private function. Anyway, after googling a little bit, I found out[^] that even CLR supports private virtual methods, and that this is a C# restriction :((
-
Heath Stewart wrote: Private members are only accessible to that class, so how can a derivate class override them? :wtf: What do you mean? Virtuality has nothing to do with access rights. In C++ it is perfectly legal (and useful) to override a virtual private function. Anyway, after googling a little bit, I found out[^] that even CLR supports private virtual methods, and that this is a C# restriction :((
Exactly - because it doesn't make any sense. If you RTFD for the C# specification, you'd know this. And it does have to do with access rights because a private member cannot be accessed by either the base or derivative classes so it can't be overridden. Since it can't be overridden, there's no need to mark it virtual! It may be allowed in IL, but it certainly doesn't make sense and hence C# restricts it. This explanation is even given in the C# language documentation.
Microsoft MVP, Visual C# My Articles
-
Exactly - because it doesn't make any sense. If you RTFD for the C# specification, you'd know this. And it does have to do with access rights because a private member cannot be accessed by either the base or derivative classes so it can't be overridden. Since it can't be overridden, there's no need to mark it virtual! It may be allowed in IL, but it certainly doesn't make sense and hence C# restricts it. This explanation is even given in the C# language documentation.
Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: because a private member cannot be accessed by either the base or derivative classes so it can't be overridden. I may be stupid, but why a method can't be overriden if it can not be accessed? What one has to do with another? [edit] BTW, take a look at this article[^] if you don't know why overriding private members is useful. [/edit] Also, I did read the C# language spec. but can't recall any mention of this. Would you tell me exactly where this was mentioned?
-
Heath Stewart wrote: because a private member cannot be accessed by either the base or derivative classes so it can't be overridden. I may be stupid, but why a method can't be overriden if it can not be accessed? What one has to do with another? [edit] BTW, take a look at this article[^] if you don't know why overriding private members is useful. [/edit] Also, I did read the C# language spec. but can't recall any mention of this. Would you tell me exactly where this was mentioned?
Okay, lets say for a second that C# allowed
virtual
on a private method. Fine. A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. And its not allowed in C# to useoverride
on a method or property that isn'tvirtual
. If you read through the C# Language Specification[^] on MSDN it is mentioned (perhaps implied - it's been a long time). 3.5.1 states that privates are limited to the containing class only. The rest is mentioned in 10.2.Microsoft MVP, Visual C# My Articles
-
Okay, lets say for a second that C# allowed
virtual
on a private method. Fine. A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. And its not allowed in C# to useoverride
on a method or property that isn'tvirtual
. If you read through the C# Language Specification[^] on MSDN it is mentioned (perhaps implied - it's been a long time). 3.5.1 states that privates are limited to the containing class only. The rest is mentioned in 10.2.Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. Please read the article I left a link to.
-
Heath Stewart wrote: A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. Please read the article I left a link to.
Fine. Great. Even interesting! But that's C/C++. C# is a different language and .NET presents a somewhat different concept. So IL supports it virtual privates. Most languages don't. While the C# language supports most of the CLI features, many languages don't support even half of that. If you want to know specifically why Microsoft choose not to support this construct, ask them. As far as the C# compiler goes, virtual methods calls use the
callvirt
IL instruction. But the C# compiler doesn't allow access to private members socallvirt
- as far as the C# compiler is concerned - can't call the private method. Maybe there is good reason for it, but - as I said - only the C# engineers can answer "why".Microsoft MVP, Visual C# My Articles
-
Heath Stewart wrote: A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. Please read the article I left a link to.
To satisfy my own curiosity, I sat down and threw this together and you're right - it does work:
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:1:4322:0
}
.assembly Test
{
.ver 1:0:0:0
}
.module Test.exe
.class public auto ansi Test
{
.method public hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 2
.locals init (class Test t)
newobj instance void Test2::.ctor()
stloc.0
ldloc.0
callvirt instance string Test::Print()
call void [mscorlib]System.Console::WriteLine(string)
ret
}.method public hidebysig specialname instance void .ctor() cil managed
{
ret
}.method private hidebysig virtual instance string Print() cil managed
{
.maxstack 1
ldstr "From Test (Private)"
ret
}
}
.class public auto ansi Test2 extends Test
{
.method public hidebysig specialname instance void .ctor() cil managed
{
ret
}.method private hidebysig virtual instance string Print() cil managed
{
.maxstack 1
ldstr "From Test2 (Private)"
ret
}
}Microsoft MVP, Visual C# My Articles
-
Okay, lets say for a second that C# allowed
virtual
on a private method. Fine. A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. And its not allowed in C# to useoverride
on a method or property that isn'tvirtual
. If you read through the C# Language Specification[^] on MSDN it is mentioned (perhaps implied - it's been a long time). 3.5.1 states that privates are limited to the containing class only. The rest is mentioned in 10.2.Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. You missed one VERY VERY important other method... The fact that an enclosed class has access to all its contained class's members, thus infact allowing a private member to be theoritcally marked virtual and to be overriden if the enclosed class derives from it's contained class. Make sense?
class Test
{
virtual void TestMethod(){}
class Test2 : Test
{
override void TestMethod(){}
}
}In my opinion this should be allowed! leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
Heath Stewart wrote: A private method cannot be accessed (except through reflection) by neither the base class or any derivative classes. This means you can't override it because it can't even be seen by either of those classes. You missed one VERY VERY important other method... The fact that an enclosed class has access to all its contained class's members, thus infact allowing a private member to be theoritcally marked virtual and to be overriden if the enclosed class derives from it's contained class. Make sense?
class Test
{
virtual void TestMethod(){}
class Test2 : Test
{
override void TestMethod(){}
}
}In my opinion this should be allowed! leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.I didn't miss nested class definitions, merely forgot them during this discussion. If either of you want this supported by C#, why not email Microsoft? While they're adding functionality to to the language right now, it would be a good time.
Microsoft MVP, Visual C# My Articles
-
I didn't miss nested class definitions, merely forgot them during this discussion. If either of you want this supported by C#, why not email Microsoft? While they're adding functionality to to the language right now, it would be a good time.
Microsoft MVP, Visual C# My Articles