in a bool 2d array, does Initialize set everything to false?

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

    #1

    in a bool 2d array, does Initialize set everything to false?

    Basically what the subject says...
    "in a bool 2d array, does Initialize set everything to false?"
  • Matt

    #2
    Re: in a bool 2d array, does Initialize set everything to false?


    Benny Raymond wrote:[color=blue]
    > Basically what the subject says...
    > "in a bool 2d array, does Initialize set everything to false?"[/color]

    Creating a 2d array:

    bool[,] myBools = new bool[5,5];

    will initialize the values (all 25 of them) to false.

    is that what you are asking?

    matt

    Comment

    • Bruce Wood

      #3
      Re: in a bool 2d array, does Initialize set everything to false?

      Yes, although I prefer not to depend upon that behaviour. A quick trip
      through the array to initialize each element to false seems a small
      price to pay to make my code easier for others (who may not be as
      familiar with the finer points of C#) to read.

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: in a bool 2d array, does Initialize set everything to false?

        Hi,

        bool is a valued type, therefore it will always has a value, if you do not
        assign something it will hold a "default" value. For bool is false , so when
        you create a new array it will be initialized to false, you have to do
        nothing else.
        Now for completeness , let say that you want to initialize it to true , in
        this case you will have to iterate in the arrays , there is no other way
        (AFAIK) of doing it . Of course without using unsafe code.


        cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        "Benny Raymond" <benny@pocketro cks.com> wrote in message
        news:%2375BepNB GHA.3064@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Basically what the subject says...
        > "in a bool 2d array, does Initialize set everything to false?"[/color]


        Comment

        • Stoitcho Goutsev \(100\) [C# MVP]

          #5
          Re: in a bool 2d array, does Initialize set everything to false?

          Benny,

          There two types of arrays 2d arrays - multidimensiona l arrays and jagged
          arrays jagged. The former are declared as
          bool[,] array;
          and the latter
          bool[][] jaggedArray;

          The multidimensiona l array will be initialized with *false*; the jagged
          array creation involves more code but at the end of the day will be
          initialized with *false* too.


          --

          Stoitcho Goutsev (100) [C# MVP]

          "Benny Raymond" <benny@pocketro cks.com> wrote in message
          news:%2375BepNB GHA.3064@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > Basically what the subject says...
          > "in a bool 2d array, does Initialize set everything to false?"[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: in a bool 2d array, does Initialize set everything to false?

            Bruce Wood <brucewood@cana da.com> wrote:[color=blue]
            > Yes, although I prefer not to depend upon that behaviour. A quick trip
            > through the array to initialize each element to false seems a small
            > price to pay to make my code easier for others (who may not be as
            > familiar with the finer points of C#) to read.[/color]

            Personally I'd prefer to rely on the specified behaviour - if I saw a
            loop initialising an array, I'd take a few looks at it to check it was
            *actually* a redundant bit of code...

            While it's reasonable to try to avoid making readers have to know the
            finer points of C#, I don't think array initialization is a
            particularly advanced topic.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            Working...