Seald classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • > Adrian

    Seald classes

    When classes are not being inherited from. Is there an advantage to be
    gained by making them "sealed", and, if so, what is that advantage?

    Thank you,
    Adrian.


  • Jeroen

    #2
    Re: Seald classes

    Adrian < wrote:
    When classes are not being inherited from. Is there an advantage to be
    gained by making them "sealed", and, if so, what is that advantage?
    Maybe there's an advantage to think of, but as I learned it you seal
    classes to make sure nothing inherits from it, not that you seal
    classes that are not inherited from.

    -Jeroen

    Comment

    • Ciaran O''Donnell

      #3
      RE: Seald classes

      The could be a performance advantage as if the CLR KNOWs for definate that
      nothing is going to inherit and override methods it can make performance
      optimizations like methods inlining etc without any risk. It depends on you
      code and its structure and what optimizations your framework version has in
      it but it something shouldn't be inherited from then mark it as sealed.
      Is a class can be a base, make sure to mark only methods/fields that need to
      be protected and virtual as such.

      Ciaran O'Donnell



      "Adrian <" wrote:
      When classes are not being inherited from. Is there an advantage to be
      gained by making them "sealed", and, if so, what is that advantage?
      >
      Thank you,
      Adrian.
      >
      >
      >

      Comment

      • Bryan

        #4
        RE: Seald classes

        It simplifies the virtual method table. At compile time, it is not possible
        to determine the actual method that will be called on a virtual method.
        Because of this, the CLR has to do a virtual table lookup. There is some
        overhead in doing this. If a class is sealed, then it may be possible to
        avoid this. Something like this.

        ParentOfSealedC lass p = new ParentOfSealedC lass();
        p.SomeVirtualMe thod();
        SealedClass s = new ParentOfSealedC lass();
        s.SomeVirtualMe thod();

        In this case, when p.SomeVirtualMe thod(), it is possible that p could be a
        subclass of ParentOfSealedC lass and the method call cannot be optimized. But
        s.SomeVirtualMe thod() can be optimized. SealedClass cannot have children.
        The exact methods that are called on an instance of SealedClass are known at
        compile time. Because of this, s.SomeVirtualMe thod() will be a little faster.

        Also keep in mind that you can seal just a method or property in a class.
        This will do exactly the same kind of thing, just on a method or property
        basis.


        "Adrian <" wrote:
        When classes are not being inherited from. Is there an advantage to be
        gained by making them "sealed", and, if so, what is that advantage?
        >
        Thank you,
        Adrian.
        >
        >
        >

        Comment

        • ssamuel

          #5
          Re: Seald classes

          Adrian,

          Aside from the technical and performance benefits, it prevents
          inheritance. If you're distributing a binary package to customers, you
          can use sealed to make sure that they can't tamper with things that
          should be untouched.


          Stephan


          Adrian < wrote:
          When classes are not being inherited from. Is there an advantage to be
          gained by making them "sealed", and, if so, what is that advantage?
          >
          Thank you,
          Adrian.

          Comment

          • > Adrian

            #6
            Re: Seald classes

            "Jeroen" <mercuros@gmail .comwrote in message
            news:1163761244 .084594.323030@ b28g2000cwb.goo glegroups.com.. .
            <snipped you seal classes to make sure nothing inherits from it, not
            that you seal
            classes that are not inherited from.
            >
            -Jeroen
            "Ciaran O''Donnell" <CiaranODonnell @discussions.mi crosoft.comwrot e in
            message news:C5D36332-058A-412D-BF42-400C92193F09@mi crosoft.com...
            There could be a performance advantage <snipped>
            Ciaran O'Donnell
            "Bryan" <Bryan@discussi ons.microsoft.c omwrote in message
            news:05F1F682-657F-4EE6-96B6-0239C55D97A7@mi crosoft.com...
            It simplifies the virtual method table <snipped>
            "ssamuel" <ssamuel@gmail. comwrote in message
            news:1163778968 .739305.252970@ f16g2000cwb.goo glegroups.com.. .
            Adrian,
            >
            <snippedyou can use sealed to make sure that they can't tamper with
            things that
            should be untouched.
            >
            Stephan
            *************** *************** *********
            M a n y t h a n k s,
            Adrian



            Comment

            Working...