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. Class field initialisation question.

Class field initialisation question.

Scheduled Pinned Locked Moved C#
question
7 Posts 6 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.
  • D Offline
    D Offline
    Dr Herbie
    wrote on last edited by
    #1

    Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

    N N L N H 6 Replies Last reply
    0
    • D Dr Herbie

      Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

      N Offline
      N Offline
      Norman Timo
      wrote on last edited by
      #2

      I think that the first method is much better too, because it is possible that you have problems to initialize the variable at a later time. (perhaps in a try-catch chapter -> than you are not able to use the new instanced variable out of this chapter) With the first method a variable is certainly instanced which is easier to handle. So it´s also easier and safer to check if a variable is set to an invalid value than to check if this variable is instanced. So if I´m wrong please rectify me... Norman-Timo

      1 Reply Last reply
      0
      • D Dr Herbie

        Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        I don't believe there is any technical reason to prefer the second method (Unless you're a VB programmer;P). As you point out the first method gaurantees the variable will be initialed to a useful value no matter which constructor you use.

        1 Reply Last reply
        0
        • D Dr Herbie

          Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

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

          Same thing :) top secret xacc-ide 0.0.1

          1 Reply Last reply
          0
          • D Dr Herbie

            Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            I would suggest you read Quiz: Type Constructors [^] by Brad Abrams[^] who is a Lead Program Manager for the CLR. - Nick Parker
            My Blog | My Articles

            1 Reply Last reply
            0
            • D Dr Herbie

              Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

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

              It compiles down to the same thing. If you don't declare a default constructor, one is defined for you anyway and instance fields are initialized in it. The only difference is that instance fields initialized outside the default constructor - when compiled - are initialized before base.ctor() is called. Any fields you initialize explicitly in your default constructor are initialized after the call to base.ctor() (compiled into your default constructor automatically). Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

              1 Reply Last reply
              0
              • D Dr Herbie

                Hi folks, OK, what I'm interested in are the technical pros and cons of data initialisation in a classes. I.e. which is better? class Test { private int mInt = -1; Test() { // Do nothing } } or class Test { private int mInt; Test() { mInt = -1; } } The reason I ask is that I usually have several constructors on my classes and I prefer the first method so I can guarantee that whichever constructor I'm entering, all the fields are initialised the same way. But are there technical reasons not to do this? Dr Herbie Remember, half the people out there have below average IQs.

                D Offline
                D Offline
                Dr Herbie
                wrote on last edited by
                #7

                Thanks for the replies, it has put my mind at rest :) Dr Herbie Remember, half the people out there have below average IQs.

                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