Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. No private virtual methods?

No private virtual methods?

Scheduled Pinned Locked Moved C#
csharphelpquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #1

    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.

    K H 2 Replies Last reply
    0
    • N Nemanja Trifunovic

      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.

      K Offline
      K Offline
      Kentamanos
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • N Nemanja Trifunovic

        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.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Use protected. Private members are only accessible to that class, so how can a derivate class override them?

        Microsoft MVP, Visual C# My Articles

        N 1 Reply Last reply
        0
        • H Heath Stewart

          Use protected. Private members are only accessible to that class, so how can a derivate class override them?

          Microsoft MVP, Visual C# My Articles

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

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

          H 1 Reply Last reply
          0
          • N Nemanja Trifunovic

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

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • H Heath Stewart

              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

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              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?

              H 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                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?

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                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 use override on a method or property that isn't virtual. 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

                N L 2 Replies Last reply
                0
                • H Heath Stewart

                  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 use override on a method or property that isn't virtual. 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

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #8

                  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.

                  H 2 Replies Last reply
                  0
                  • N Nemanja Trifunovic

                    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.

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    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 so callvirt - 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

                    1 Reply Last reply
                    0
                    • N Nemanja Trifunovic

                      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.

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • H Heath Stewart

                        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 use override on a method or property that isn't virtual. 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

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #11

                        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.

                        H 1 Reply Last reply
                        0
                        • L leppie

                          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.

                          H Offline
                          H Offline
                          Heath Stewart
                          wrote on last edited by
                          #12

                          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

                          L 1 Reply Last reply
                          0
                          • H Heath Stewart

                            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

                            L Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #13

                            Nahh i'm too lazy and my email is still broken.... anyways internal protected does enuf for me :) leppie::AllocCPArticle("Zee blog");
                            Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups