By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,050 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,050 IT Pros & Developers. It's quick & easy.

declare array with a default value?

P: n/a
Hi,

Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I
can't quite see what I want to do.
I want an array of Booleans of length 102, all initially "false".
I have this so far:

public bool[] Monitors = new bool[102];

but if I add on the end of that : {false}, I get a compile error of "Invalid
rank specifier, expected ',' or ']'.
Now, I presume there is a quicker way than typing
{false,false,false,false.... 100 times? :)
Or does it default to false anyway?
Sep 22 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Luckily for you, booleans are false by default anyway.

Ciaran O'Donnell

"james" wrote:
Hi,

Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I
can't quite see what I want to do.
I want an array of Booleans of length 102, all initially "false".
I have this so far:

public bool[] Monitors = new bool[102];

but if I add on the end of that : {false}, I get a compile error of "Invalid
rank specifier, expected ',' or ']'.
Now, I presume there is a quicker way than typing
{false,false,false,false.... 100 times? :)
Or does it default to false anyway?
Sep 22 '06 #2

P: n/a
Hi,
Or does it default to false anyway?
You are right. When you initialize a new array of bool's all the
elements have default values set to False.
private void button1_Click(object sender, EventArgs e)
{

bool[] arr = new bool[3];
foreach (bool b in arr)
{
MessageBox.Show(b.ToString());
}
}

Try this code in a windows application for a button click event. You
can change the array size.

Thanks.

Sep 22 '06 #3

P: n/a

"coolCoder" <an*********************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,
>Or does it default to false anyway?

You are right. When you initialize a new array of bool's all the
elements have default values set to False.
private void button1_Click(object sender, EventArgs e)
{

bool[] arr = new bool[3];
foreach (bool b in arr)
{
MessageBox.Show(b.ToString());
}
}

Try this code in a windows application for a button click event. You
can change the array size.
Thanks to both replies - false is handy :)
My other option was to simply loop through and set them all to false before
the *useful* stuff that will happen, but that seemed a horrible waste of cpu
cycles. A Default of false suits me fine.

James.
Sep 22 '06 #4

P: n/a
Hi James,

All values types (int, bool etc) are set to their default level, which
usually is 0 or false. References will be null. If you want another
value you need to loop through and set it yourself.
On Fri, 22 Sep 2006 13:54:28 +0200, james <ja***@com.comwrote:
>
"coolCoder" <an*********************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>Hi,
>>Or does it default to false anyway?

You are right. When you initialize a new array of bool's all the
elements have default values set to False.
private void button1_Click(object sender, EventArgs e)
{

bool[] arr = new bool[3];
foreach (bool b in arr)
{
MessageBox.Show(b.ToString());
}
}

Try this code in a windows application for a button click event. You
can change the array size.

Thanks to both replies - false is handy :)
My other option was to simply loop through and set them all to false
before
the *useful* stuff that will happen, but that seemed a horrible waste of
cpu
cycles. A Default of false suits me fine.

James.



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 22 '06 #5

P: n/a
Hi,

In general, if you wanted to initialize your array of value types to a
specific value other than the default you would need to loop through the
array.

for (int x=0; x<Monitors.Length; ++x )
{
Monitors[x] = true;
}

"james" <ja***@com.comwrote in message
news:45**********************@news.zen.co.uk...
Hi,

Just looking here: http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx
I can't quite see what I want to do.
I want an array of Booleans of length 102, all initially "false".
I have this so far:

public bool[] Monitors = new bool[102];

but if I add on the end of that : {false}, I get a compile error of
"Invalid rank specifier, expected ',' or ']'.
Now, I presume there is a quicker way than typing
{false,false,false,false.... 100 times? :)
Or does it default to false anyway?

Sep 22 '06 #6

P: n/a
"james" <ja***@com.comwrote in message
news:45**********************@news.zen.co.uk...
Hi,

Just looking here:
http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx I can't quite
see what I want to do.
I want an array of Booleans of length 102, all initially "false".
I have this so far:

public bool[] Monitors = new bool[102];
Have you looked into using the BitArray class instead of an Array of
bits?

Bill
Sep 22 '06 #7

P: n/a

"Bill Butler" <qw****@asdf.comwrote in message
news:wYQQg.4194$Bh.2872@trnddc03...
>
Have you looked into using the BitArray class instead of an Array of bits?
I haven't - but I'll investigate. Presumably it's more efficient?
Sep 22 '06 #8

P: n/a
"james" <ja***@com.comwrote in message
news:45***********************@news.zen.co.uk...
>
"Bill Butler" <qw****@asdf.comwrote in message
news:wYQQg.4194$Bh.2872@trnddc03...
>>
Have you looked into using the BitArray class instead of an Array of
bits?

I haven't - but I'll investigate. Presumably it's more efficient?
forgetting about overhead, an Array of bools takes up a whole byte for
every bit stored. It also has some constructors that ease
initialization.There are other features surrounding bit twiddling as
well.

Bill
Sep 23 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.