foreach不适用于一系列bool? [英] foreach doesn't work with array of bools?

查看:80
本文介绍了foreach不适用于一系列bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我写了下面的代码。我想要一系列bool,我想要

将它们初始化为假。


bool [] bits = new bool [10];

foreach(bool bit in bits)

{

bit = false;

}


编译器在bit = false;上抱怨声明该位是只读的。

如果我使用索引迭代,它编译就好了:


for(int i = 0; i< ; bits.Length; i ++)

{

bits [i] = false;

}


编译得很好。发生了什么?


此外,无论如何,bool数组似乎初始化为假。我可以指望这个吗?我是否应该担心初始化它们?


谢谢。

解决方案

bool [] bits = new bool [10];
foreach(bool bit in bits)
{
bit = false;
}


在此代码段中,位是指向位

数组中的一个值的指针。它是一个只读指针,所以你不能写它。

for(int i = 0; i< bits.Length; i ++)
{
bits [i] = false;
}




这使事情变得更容易处理。基本上,您直接在此代码段中设置数组成员的

值,而不是尝试通过指针访问它来获得
。因此保留了您创建的数组

的读/写性质。


HTH


Joe Rattz写道:


嗯,我写了下面的代码。我想要一系列bool,我想将它们初始化为假。

bool [] bits = new bool [10];
foreach(bool bit in bits)
{
bit = false;
}

编译器抱怨bit = false;声明该位是只读的。
如果我使用索引迭代,它编译就好了:

for(int i = 0; i< bits.Length; i ++)
{
bits [i] = false;
}

编译得很好。发生了什么?

此外,无论如何,bool阵列似乎已经初步化为假。我可以依靠吗?我是否应该担心初始化它们?




构造的bool的默认值是''false'':

http://msdn.microsoft.com/library/en。 ..nstructors.asp


另一种分配和初始化的方法是:


bool [] bits = new bool [ ] {false,false,false,false / * etc * /};


这允许您在构造中指定不同的值,例如:


bool [] bits = new bool [] {true,false,true,false / * etc * /};


Joe,

发生了什么事?


正是编译器所说的。 foreach语句中的变量是

只读。


此外,无论如何,bool数组似乎初始化为false。我可以依靠吗?


是的,你可以。创建新数组时,所有元素都具有

默认初始值。对于布尔来说,这是假的。


我是否应该担心初始化它们?




我不会浪费CPU周期。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


Hmmm, I wrote the following code. I want an array of bools and I want to
intialize them to false.

bool[] bits = new bool[10];
foreach(bool bit in bits)
{
bit = false;
}

The compiler complains on the "bit = false;" stating that bit is read-only.
If I iterate using an index though, it compiles just fine:

for(int i = 0; i < bits.Length; i++)
{
bits[i] = false;
}

That compiles just fine. What is going on?

Also, the array of bools seems to have intialized to false anyway. Can I
count on that? Should I even worry about initializing them?

Thanks.

解决方案

bool[] bits = new bool[10];
foreach(bool bit in bits)
{
bit = false;
}
In this snippet, "bit" is a pointer to one of the values in the bits
array. It is a read-only pointer, though, so you cannot write to it.
for(int i = 0; i < bits.Length; i++)
{
bits[i] = false;
}



This makes things easier to deal with. Basically, you are setting the
value of an array member directly in this snippet, rather than trying
to access it through a pointer. The read/write nature of the array
you created is therefore preserved.

HTH


Joe Rattz wrote:


Hmmm, I wrote the following code. I want an array of bools and I want to
intialize them to false.

bool[] bits = new bool[10];
foreach(bool bit in bits)
{
bit = false;
}

The compiler complains on the "bit = false;" stating that bit is read-only.
If I iterate using an index though, it compiles just fine:

for(int i = 0; i < bits.Length; i++)
{
bits[i] = false;
}

That compiles just fine. What is going on?

Also, the array of bools seems to have intialized to false anyway. Can I
count on that? Should I even worry about initializing them?



The default value for a constructed bool is ''false'':

http://msdn.microsoft.com/library/en...nstructors.asp

An alternative method of alloc and initialize is:

bool[] bits = new bool[] { false, false, false, false /*etc*/ };

This allows you to specify differing values in construction, such as:

bool[] bits = new bool[] { true, false, true, false /*etc*/ };


Joe,

What is going on?
Exactly what the compiler says. The variable in a foreach statement is
read only.

Also, the array of bools seems to have intialized to false anyway. Can I
count on that?
Yes you can. When you create a new array, all elements have their
default initial value. For bool, that''s false.

Should I even worry about initializing them?



I wouldn''t waste CPU cycles on it.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


这篇关于foreach不适用于一系列bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆