base() question :-)

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

    base() question :-)

    Hi everyone,

    Quick question:

    If I don't use base() in a subclass's construcor, will the base classes
    constructor be called at any point. It's just, I would have thought that the
    base class constructor would always need to be called before anything else
    seeing as the subclass may well depend on functionality and variables
    created in the base class.

    When base() is used, does it get called before anything else or is there a
    way to control when it gets called.

    Simon


  • Marina

    #2
    Re: base() question :-)

    If you don't call it expcitly, it will never be called. If you do want it
    called, it will be called prior to any other code in the subclass's
    constructor.

    "Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
    news:OnEQrNI2EH A.208@TK2MSFTNG P12.phx.gbl...[color=blue]
    > Hi everyone,
    >
    > Quick question:
    >
    > If I don't use base() in a subclass's construcor, will the base classes
    > constructor be called at any point. It's just, I would have thought that[/color]
    the[color=blue]
    > base class constructor would always need to be called before anything else
    > seeing as the subclass may well depend on functionality and variables
    > created in the base class.
    >
    > When base() is used, does it get called before anything else or is there a
    > way to control when it gets called.
    >
    > Simon
    >
    >[/color]


    Comment

    • Dennis Myrén

      #3
      Re: base() question :-)

      That is not correct.
      The base class constructor is always called.
      If you do not explicitly specify which one,
      the default constructor is called.
      The default constructor is the one which do not have parameters.
      So, we can verify this by not providing a default constructor for a test
      base class:
      class Base
      {
      public Base( int id ) {}
      }
      class Concrete : Base
      {
      public Concrete() {}
      }

      This will result in compilation error:
      "No overload for method Base takes 0 arguments"
      However, if we remove the parameter from Base constructor,
      the code will compile.

      --
      Regards,
      Dennis JD Myrén
      Oslo Kodebureau
      "Marina" <someone@nospam .com> wrote in message
      news:eZIDSUI2EH A.2568@TK2MSFTN GP11.phx.gbl...[color=blue]
      > If you don't call it expcitly, it will never be called. If you do want it
      > called, it will be called prior to any other code in the subclass's
      > constructor.
      >
      > "Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
      > news:OnEQrNI2EH A.208@TK2MSFTNG P12.phx.gbl...[color=green]
      >> Hi everyone,
      >>
      >> Quick question:
      >>
      >> If I don't use base() in a subclass's construcor, will the base classes
      >> constructor be called at any point. It's just, I would have thought that[/color]
      > the[color=green]
      >> base class constructor would always need to be called before anything
      >> else
      >> seeing as the subclass may well depend on functionality and variables
      >> created in the base class.
      >>
      >> When base() is used, does it get called before anything else or is there
      >> a
      >> way to control when it gets called.
      >>
      >> Simon
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: base() question :-)

        Marina <someone@nospam .com> wrote:[color=blue]
        > If you don't call it expcitly, it will never be called.[/color]

        From the C# spec, section 17.10.1:

        <quote>
        If an instance constructor has no constructor initializer, a
        constructor initializer of the form base() is implicitly provided.
        </quote>

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Flip

          #5
          Re: base() question :-)

          "Marina" <someone@nospam .com> wrote in message
          news:eZIDSUI2EH A.2568@TK2MSFTN GP11.phx.gbl...[color=blue]
          > If you don't call it expcitly, it will never be called. If you do want it
          > called, it will be called prior to any other code in the subclass's
          > constructor.[/color]
          Howdy, I just thought I would jump in here too with my two cents. :>

          The advantage of this is with constructor chaining (I'm sure there are more
          reasons, but this is one I like to use it for :>). In other words, you're
          overloading constructors, gotta love OOP! :>

          Take a look here for Chaining.



          Comment

          • Simon

            #6
            Re: base() question :-)

            Hi everyone,

            If the base constructor is implicitly called whether you specify it or not,
            then what is the point of base()?

            Thanks for your help so far everyone. Much appreciated

            tce


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: base() question :-)

              Simon <sh856531@micro softs_free_emai l_service.com> wrote:[color=blue]
              > If the base constructor is implicitly called whether you specify it or not,
              > then what is the point of base()?[/color]

              Only clarity - just specifying it (without any parameters) is basically
              redundant.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Ben Lucas

                #8
                Re: base() question :-)

                Because sometimes you want to call a base constructor that has variables in
                it.
                For example:

                public class Point
                {
                public int X;
                public int Y;

                public Point(int xValue, int yValue)
                {
                X = xValue;
                Y = yValue;
                }
                }

                public class ExtendedPoint : Point
                {
                public ExtendedPoint(i nt xValue, int yValue) : base(xValue, yValue)
                {
                }
                }

                --
                Ben Lucas
                Lead Developer
                Solien Technology, Inc.
                Solien Technolgy is a technology consulting firm in Santa Monica. We specialize in SharePoint, portals, business process improvement and custom development.


                "Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
                news:eiowuSK2EH A.1124@tk2msftn gp13.phx.gbl...[color=blue]
                > Hi everyone,
                >
                > If the base constructor is implicitly called whether you specify it or
                > not, then what is the point of base()?
                >
                > Thanks for your help so far everyone. Much appreciated
                >
                > tce
                >[/color]


                Comment

                • Simon

                  #9
                  Re: base() question :-)

                  Gotcha. Thanks guys

                  Simon


                  Comment

                  Working...