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. Why c# do not support Multiple Inheritance

Why c# do not support Multiple Inheritance

Scheduled Pinned Locked Moved C#
csharpoop
21 Posts 11 Posters 2 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.
  • H Himanshu Yadav

    Why is it so in c#

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #7

    Well, one reason it's so is because .NET doesn't support multiple inheritance.

    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    P 1 Reply Last reply
    0
    • H Himanshu Yadav

      Why is it so in c#

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #8

      To answer your original question

      Himanshu Yadav wrote:

      Re: WHY GIRLS DONT LIKE TO MARRY SOFTWARE ENGINEERS ?????

      They must have read your posts. You might try posting anonymously though.

      P 1 Reply Last reply
      0
      • H Himanshu Yadav

        Why is it so in c#

        D Offline
        D Offline
        Dave Doknjas
        wrote on last edited by
        #9

        There's a reason most modern languages don't support multiple inheritance.

        David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com Instant C# - VB to C# Converter Instant VB - C# to VB Converter

        1 Reply Last reply
        0
        • H Himanshu Yadav

          Why is it so in c#

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #10

          Himanshu Yadav wrote:

          Why c# do not support Multiple Inheritance

          Why do you think it matters?

          1 Reply Last reply
          0
          • L Lost User

            To answer your original question

            Himanshu Yadav wrote:

            Re: WHY GIRLS DONT LIKE TO MARRY SOFTWARE ENGINEERS ?????

            They must have read your posts. You might try posting anonymously though.

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #11

            :thumbsup: You made my day. :-)

            1 Reply Last reply
            0
            • H Himanshu Yadav

              Why is it so in c#

              P Offline
              P Offline
              pasztorpisti
              wrote on last edited by
              #12

              My opinion: Because multiple inheritance brings some problems to the langauge and its easier to go with single line inheritance and multiple interfaces. As an example I would mention C++ that supports multiple inheritance but has no interfaces. Because of this sometimes it occurs that you are forced to use virtual inheritance that usually isn't a clean solution compared to single line inheritance + interfaces. My experience is that virtual inheritance is avoided in C++ whenever possible because noone likes it. Besides that multiple inheritance can come handy but doesn't add more than the single line inheritance+interfaces solution that is automatically immune to virtual inheritance problems. Google "virtual inheritance" to find out more about this problem.

              1 Reply Last reply
              0
              • P Pete OHanlon

                Well, one reason it's so is because .NET doesn't support multiple inheritance.

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                P Offline
                P Offline
                pasztorpisti
                wrote on last edited by
                #13

                If we take a look at how Visual C++ implements multiple inheritance (on binary level) it becomes obvious that even the binary stuff under C++ is built up from the composition of single line inheritances (the reason for multiple vtables in the same object). So we can consider multiple inheritance as a language feature, a "syntactic sugar" that is somewhat a mixture of single line inheritance and object composition in "machine code". It would be possible to create a language supporting multiple inheritance on top of .Net with the same principles but multiple inheritance brings up some design-related questions. I think the single line inheritance is rather a design decision in C#.

                P 1 Reply Last reply
                0
                • H Himanshu Yadav

                  Why is it so in c#

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #14

                  In contrast to some "experts" here on CP, I worked with a language which supports multiple inheritance: C++ (i.e. non-managed C++). Hence I think that your question is an appropriate question, it does not deserve downvoting. Let me give you an example which shows where multiple inheritance is a pain. Imagine two base classes which have a function with the same name.

                  public class FirstClass
                  {
                  public virtual void DoSomething()
                  {
                  //some code
                  }
                  }

                  and

                  public class SecondClass
                  {
                  public virtual void DoSomething()
                  {
                  //some other code
                  }
                  }

                  When you create a class inheriting from both of these classes

                  public class CombinedClass: FirstClass, SecondClass
                  {
                  //some other code
                  }

                  it inherits the DoSomething() method from both FirstClass and SecondClass. When you call DoSomething() on an instance of CombinedClass

                  CombinedClass c = new CombinedClass();
                  c.DoSomething()

                  which of the DoSomething() methods do you call - FirstClass.DoSomething() or SecondClass.DoSomething()? Other object oriented languages may offer more features which are not supported in the .Net world, e.g. inheritance of static functions and properties.

                  H 1 Reply Last reply
                  0
                  • P pasztorpisti

                    If we take a look at how Visual C++ implements multiple inheritance (on binary level) it becomes obvious that even the binary stuff under C++ is built up from the composition of single line inheritances (the reason for multiple vtables in the same object). So we can consider multiple inheritance as a language feature, a "syntactic sugar" that is somewhat a mixture of single line inheritance and object composition in "machine code". It would be possible to create a language supporting multiple inheritance on top of .Net with the same principles but multiple inheritance brings up some design-related questions. I think the single line inheritance is rather a design decision in C#.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #15

                    Actually, the reason I mentioned it in regards to .NET not supporting it is because (to keep it in line with general .NET interoperability reasons), it would have to be baked into the CLS so that other languages could interact with it. From Anders Hjelsberg: "The question is, What about multiple inheritance? And will we get it? If not, why? The easy- the cop out answer at this point is that if we were going to have it, we needed to have it in the first release because it’s way too hard to add after the fact. Even if we did add it, we could never mandate it as being part of the CLS I don’t think, meaning that every language can support it. Multiple inheritance and VB, I don’t think that’s a great cocktail. If we had multiple inheritance, if it wasn’t usable in APIs, I wonder how useful it would really be. I think also when you analyze what is it that people want to do with multiple inheritance, in the vast majority of cases I think there are simpler answers than the full multiple inheritance enchilada with virtual base classes and the diamond problem and all of that stuff." I don't just make this shizzle up after all.

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    P 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Actually, the reason I mentioned it in regards to .NET not supporting it is because (to keep it in line with general .NET interoperability reasons), it would have to be baked into the CLS so that other languages could interact with it. From Anders Hjelsberg: "The question is, What about multiple inheritance? And will we get it? If not, why? The easy- the cop out answer at this point is that if we were going to have it, we needed to have it in the first release because it’s way too hard to add after the fact. Even if we did add it, we could never mandate it as being part of the CLS I don’t think, meaning that every language can support it. Multiple inheritance and VB, I don’t think that’s a great cocktail. If we had multiple inheritance, if it wasn’t usable in APIs, I wonder how useful it would really be. I think also when you analyze what is it that people want to do with multiple inheritance, in the vast majority of cases I think there are simpler answers than the full multiple inheritance enchilada with virtual base classes and the diamond problem and all of that stuff." I don't just make this shizzle up after all.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      P Offline
                      P Offline
                      pasztorpisti
                      wrote on last edited by
                      #16

                      Yes, this was one thing I was thinking about because if you wanted to implement multiple inheritance on top of .Net then it would require you to introduce some anonymous base classes (that embody the single line inheritances inside the composite class) and then the composite class itself is just the syntactic sugar provided by the language and inaccessible by the other languages, and of course you would be unable to reach the anonymous classes (classes with auto-generated name) too from other languages. Only some parts of your code (with single line inheritance) could be reached from other languages. My statement about the possibility of providing multiple inheritance in a language on top of .Net still holds. The fact that they allowed just single line inheritance in .Net that is an intersection between languages was a wise decision though, read a list of reasonings about that somewhere.

                      P 1 Reply Last reply
                      0
                      • P pasztorpisti

                        Yes, this was one thing I was thinking about because if you wanted to implement multiple inheritance on top of .Net then it would require you to introduce some anonymous base classes (that embody the single line inheritances inside the composite class) and then the composite class itself is just the syntactic sugar provided by the language and inaccessible by the other languages, and of course you would be unable to reach the anonymous classes (classes with auto-generated name) too from other languages. Only some parts of your code (with single line inheritance) could be reached from other languages. My statement about the possibility of providing multiple inheritance in a language on top of .Net still holds. The fact that they allowed just single line inheritance in .Net that is an intersection between languages was a wise decision though, read a list of reasonings about that somewhere.

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #17

                        pasztorpisti wrote:

                        read a list of reasonings about that somewhere

                        Indeed - it's not a feature I missed when I moved over from C++ to C#. Now, if C# supported proper templates, I would be happy.

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        P 1 Reply Last reply
                        0
                        • P Pete OHanlon

                          pasztorpisti wrote:

                          read a list of reasonings about that somewhere

                          Indeed - it's not a feature I missed when I moved over from C++ to C#. Now, if C# supported proper templates, I would be happy.

                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                          P Offline
                          P Offline
                          pasztorpisti
                          wrote on last edited by
                          #18

                          What features would you like to have from C++ templates? I think they are overcomplicated, and since the C++ specification describes just how they should work - and nothing about implementation - different C++ compilers handle them differently. I could tell you at least 2 big horror stories about this (one bug and one source code porting).

                          P 1 Reply Last reply
                          0
                          • P pasztorpisti

                            What features would you like to have from C++ templates? I think they are overcomplicated, and since the C++ specification describes just how they should work - and nothing about implementation - different C++ compilers handle them differently. I could tell you at least 2 big horror stories about this (one bug and one source code porting).

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #19

                            pasztorpisti wrote:

                            the C++ specification describes just how they should work - and nothing about implementation - different C++ compilers handle them differently

                            The key thing would be that .NET is standardised, so there would be no ambiguity. Some things I'd like - arithmetic operator overloading, explicit and partial specialisation.

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                            P 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              pasztorpisti wrote:

                              the C++ specification describes just how they should work - and nothing about implementation - different C++ compilers handle them differently

                              The key thing would be that .NET is standardised, so there would be no ambiguity. Some things I'd like - arithmetic operator overloading, explicit and partial specialisation.

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                              P Offline
                              P Offline
                              pasztorpisti
                              wrote on last edited by
                              #20

                              The arithmetic part is indeed a huge problem in C#. About the template specialization: I think they are rarely used in C++ and even in those cases I've often use of it without good reason. A nice use of it is optimization like std::vector with packed bits, or code beautification. In other cases where it was used some other simpler solution could also do the job for me.

                              1 Reply Last reply
                              0
                              • B Bernhard Hiller

                                In contrast to some "experts" here on CP, I worked with a language which supports multiple inheritance: C++ (i.e. non-managed C++). Hence I think that your question is an appropriate question, it does not deserve downvoting. Let me give you an example which shows where multiple inheritance is a pain. Imagine two base classes which have a function with the same name.

                                public class FirstClass
                                {
                                public virtual void DoSomething()
                                {
                                //some code
                                }
                                }

                                and

                                public class SecondClass
                                {
                                public virtual void DoSomething()
                                {
                                //some other code
                                }
                                }

                                When you create a class inheriting from both of these classes

                                public class CombinedClass: FirstClass, SecondClass
                                {
                                //some other code
                                }

                                it inherits the DoSomething() method from both FirstClass and SecondClass. When you call DoSomething() on an instance of CombinedClass

                                CombinedClass c = new CombinedClass();
                                c.DoSomething()

                                which of the DoSomething() methods do you call - FirstClass.DoSomething() or SecondClass.DoSomething()? Other object oriented languages may offer more features which are not supported in the .Net world, e.g. inheritance of static functions and properties.

                                H Offline
                                H Offline
                                Himanshu Yadav
                                wrote on last edited by
                                #21

                                I like your example and way of thinking great!!!! Thanks

                                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