473,414 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

foreach on an object[]

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

Nov 15 '05 #1
10 13511
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

Nov 15 '05 #2
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
Nov 15 '05 #3
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

Nov 15 '05 #4
Surely all you need to do is:

object[] buffer = new object[3];

....and you'll have an array of three object references, all of which are
null.
"Leo Lin" <le*****@hotmail.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
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


Nov 15 '05 #5
You can do that for large arrays too, Jeff!
AHAHAHAHAHAHAHAHAHAHAHA
HAHAHAHAHAHAHAHAHAHAHAH
AHAHAHAHAHAHAHAHAAHAHAH
AHAHAHAHAHAHAAHAHA... *crack*

"Jeff" <je**@nowhere.com> wrote in message
news:#x**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #6
True, but who wants to code something like:

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

:-)

"Humanity" <us******@domain.com> wrote in message
news:O3***************@TK2MSFTNGP11.phx.gbl...
You can do that for large arrays too, Jeff!
AHAHAHAHAHAHAHAHAHAHAHA
HAHAHAHAHAHAHAHAHAHAHAH
AHAHAHAHAHAHAHAHAAHAHAH
AHAHAHAHAHAHAAHAHA... *crack*

"Jeff" <je**@nowhere.com> wrote in message
news:#x**************@TK2MSFTNGP11.phx.gbl...
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



Nov 15 '05 #7
Allocating new arrays is not always the right design choice (even thou it is
easy), especially in loops when you would need to make many allocations
(i.e. streams, network objects, etc.) and could reuse the array.

--
William Stacey

"Stu Smith" <st*****@remove.digita.com> wrote in message
news:#M**************@TK2MSFTNGP09.phx.gbl...
Surely all you need to do is:

object[] buffer = new object[3];

...and you'll have an array of three object references, all of which are
null.
"Leo Lin" <le*****@hotmail.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
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



Nov 15 '05 #8
Certainly not me!
I see your point :)

"Jeff" <je**@nowhere.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
True, but who wants to code something like:

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

:-)

"Humanity" <us******@domain.com> wrote in message
news:O3***************@TK2MSFTNGP11.phx.gbl...
You can do that for large arrays too, Jeff!
AHAHAHAHAHAHAHAHAHAHAHA
HAHAHAHAHAHAHAHAHAHAHAH
AHAHAHAHAHAHAHAHAAHAHAH
AHAHAHAHAHAHAAHAHA... *crack*

"Jeff" <je**@nowhere.com> wrote in message
news:#x**************@TK2MSFTNGP11.phx.gbl...
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
>
>
>



Nov 15 '05 #9

Hi William,

Yes, foreach statement does not allow the user to change the value in the
array.
I think you can initialize the array like this:

object[] buffer = new object[3];
for(int i=0;i<buffer.Length ;i++)
{
buffer[i]=null;
}

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "William Stacey" <st*****@mvps.org>
| Subject: foreach on an object[]
| Date: Mon, 29 Sep 2003 11:49:20 -0400
| Lines: 15
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <uy*************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:188008
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| 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
|
|
|
|

Nov 15 '05 #10
Thanks Jeff.

--
William Stacey, DNS MVP

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:KC**************@cpmsftngxa06.phx.gbl...

Hi William,

Yes, foreach statement does not allow the user to change the value in the
array.
I think you can initialize the array like this:

object[] buffer = new object[3];
for(int i=0;i<buffer.Length ;i++)
{
buffer[i]=null;
}

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "William Stacey" <st*****@mvps.org>
| Subject: foreach on an object[]
| Date: Mon, 29 Sep 2003 11:49:20 -0400
| Lines: 15
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Message-ID: <uy*************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:188008 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| 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
|
|
|
|

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
4
by: Sagaert Johan | last post by:
Hi Is this something for C# V2 ? Queue msgqueue =new Queue (); //fill some data in the queue here with this function & keep only the last 5 events void addtolog(string msg) {
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
3
by: Steve Barnett | last post by:
I have a collection which I need to navigate through in several different ways. Is there any way to structure my class such that I can have several different iterators? Ideally, I'd like to do...
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
3
by: Emily | last post by:
Hi, I have the following code and an unhandled exception. Code: UnitInventory inventory; foreach (object obj in list) //debugger points to list where exception occurs {
11
by: Robert W. | last post by:
Here's some pseudo-code that describes what I'm trying to do: foreach(object in collection) { if (certain-condition) collection.Remove(object); } The problem with this is that foreach gets...
8
by: trint | last post by:
I get this error: Unable to cast object of type 'System.Data.DataRowView' to type 'System.String' on this foreach and I thought it should work: foreach (string invoice in listBox2.Items) {
10
by: DoB | last post by:
Hi, Given the following declaration: object a = new object; which code is faster? 1. foreach ( object o in a )
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.