foreach on a object [] [英] foreach on an object[]

查看:66
本文介绍了foreach on a object []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能用foreach将对象[]的元素设置为null吗?这是因为o是只读的,所以
不起作用。是否只能使用for循环?


object [] buffer = new object [3];

//...fill buffer。

foreach(缓冲区中的对象o)

{

o = null;

}


-

William Stacey

解决方案

如果它是一个小阵列,你也可以这样做:


object [] buffer = {null,null,null};


--- Jeff


" William Stacey" < ST ***** @ mvps.org>在消息中写道

新闻:uy ************* @ TK2MSFTNGP10.phx.gbl ...

你能用foreach来将对象[]的元素设置为null?这个
不起作用,因为o是只读的。是否只能使用for循环?

object [] buffer = new object [3];
//...fill buffer。
foreach(缓冲区中的对象o) )
{
o = null;
}

- William Stacey



William Stacey< st ***** @ mvps.org>写道:

你能使用foreach将对象[]的元素设置为null吗?由于o是只读的,因此无法正常工作。是否只能使用for循环?

object [] buffer = new object [3];
//...fill buffer。
foreach(缓冲区中的对象o) )
{
o = null;
}




即使o不是readonly,也不会改变缓冲区本身 -

o是一个局部变量:改变它的值不会改变

的价值。


然而,那里这是一个更好的方法。我不确定在你的例子中是否

,// ..填充缓冲区评论说

foreach正在这样做,在这种情况下它'在第一个

位置是不必要的,但如果没有,只需使用Array.Clear(buffer,0,buffer.Length);


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


我认为最简单的方法是创建一个新数组

object [] buffer = new object [3];

//...fill buffer 。

buffer = new object [3];


" William Stacey" < ST ***** @ mvps.org>在消息中写道

新闻:uy ************* @ TK2MSFTNGP10.phx.gbl ...

你能用foreach来将对象[]的元素设置为null?这个
不起作用,因为o是只读的。是否只能使用for循环?

object [] buffer = new object [3];
//...fill buffer。
foreach(缓冲区中的对象o) )
{
o = null;
}

- William Stacey



Can you use a foreach to set the elements of an object[] to null? This does
not work as o is read-only. Is only way to use a for loop?

object[] buffer = new object[3];
//...fill buffer.
foreach (object o in buffer)
{
o = null;
}

--
William Stacey

解决方案

If it''s a small array, you can also do this:

object[] buffer = {null, null, null};

--- Jeff

"William Stacey" <st*****@mvps.org> wrote in message
news:uy*************@TK2MSFTNGP10.phx.gbl...

Can you use a foreach to set the elements of an object[] to null? This does not work as o is read-only. Is only way to use a for loop?

object[] buffer = new object[3];
//...fill buffer.
foreach (object o in buffer)
{
o = null;
}

--
William Stacey



William Stacey <st*****@mvps.org> wrote:

Can you use a foreach to set the elements of an object[] to null? This does
not work as o is read-only. Is only way to use a for loop?

object[] buffer = new object[3];
//...fill buffer.
foreach (object o in buffer)
{
o = null;
}



Even if o were not readonly, that would not change the buffer itself -
o is a local variable: changing its value wouldn''t change the value of
anything else.

However, there''s a much better way of doing this. I''m not sure whether
in your example, the //...fill buffer comment was saying that the
foreach was doing it, in which case it''s unnecessary in the first
place, but if not, just use Array.Clear (buffer, 0, buffer.Length);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


I think the easist way is to create a new array
object[] buffer = new object[3];
//...fill buffer.
buffer = new object[3];

"William Stacey" <st*****@mvps.org> wrote in message
news:uy*************@TK2MSFTNGP10.phx.gbl...

Can you use a foreach to set the elements of an object[] to null? This does not work as o is read-only. Is only way to use a for loop?

object[] buffer = new object[3];
//...fill buffer.
foreach (object o in buffer)
{
o = null;
}

--
William Stacey



这篇关于foreach on a object []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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