471,319 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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?"
Dec 19 '05 #1
5 11871

Benny Raymond wrote:
Basically what the subject says...
"in a bool 2d array, does Initialize set everything to false?"


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

Dec 19 '05 #2
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.

Dec 19 '05 #3
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" <be***@pocketrocks.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Basically what the subject says...
"in a bool 2d array, does Initialize set everything to false?"

Dec 19 '05 #4
Benny,

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

The multidimensional 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" <be***@pocketrocks.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Basically what the subject says...
"in a bool 2d array, does Initialize set everything to false?"

Dec 19 '05 #5
Bruce Wood <br*******@canada.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michelle | last post: by
2 posts views Thread by Harry Haller | last post: by
1 post views Thread by kelkel | last post: by
4 posts views Thread by gpg | last post: by
32 posts views Thread by Simon L | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.