declare array with a default value?

  • Thread starter Thread starter james
  • Start date Start date
J

james

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?
 
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.
 
coolCoder said:
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.
 
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.


coolCoder said:
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.
 
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 said:
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
 
Back
Top