473,499 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

foreach issue

Jay
When doing a foreach statement like the following why is the variable
read-only?

foreach (e in theArray)

What if I have an array of objects and I need to create a new instance of
the object. If the e is read-only then I have to use a for statement to
accomplish this.
Nov 16 '05 #1
4 1036
Jay,

It is read-only just to prevent you for making mistakes.

*e* (in your example) is nothing more than a local variable. You may change
its value, but it won't affect the reference kept in the collection. Having
it read only let the compiler emit an error if this happens.

e.SomeProp = ...; at the other hand is correct code. However whether it is
going to chanage the object in the collection depends on what is the type of
the collection (array or non-typed collection) and what is the type of the
item (reference or value type).
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Jay" <j@h.com> wrote in message news:a8LSc.10$xs.0@okepread02...
When doing a foreach statement like the following why is the variable
read-only?

foreach (e in theArray)

What if I have an array of objects and I need to create a new instance of
the object. If the e is read-only then I have to use a for statement to
accomplish this.

Nov 16 '05 #2
i think a better way to put it is you can't change an element of your
enumerating through, if you have an array and you want to cycle through
it, you can make changes to the list your cycling through

Stoitcho Goutsev (100) [C# MVP] wrote:
Jay,

It is read-only just to prevent you for making mistakes.

*e* (in your example) is nothing more than a local variable. You may change
its value, but it won't affect the reference kept in the collection. Having
it read only let the compiler emit an error if this happens.

e.SomeProp = ...; at the other hand is correct code. However whether it is
going to chanage the object in the collection depends on what is the type of
the collection (array or non-typed collection) and what is the type of the
item (reference or value type).

Nov 16 '05 #3
There is a HUGE thread discussing the issue of non-mutable collections while
in a for-each statement, going on right now, on this group (called For vs
For Each). If you want a long discussion of the reasons, and complaints,
that come from making the collection read-only for the duration of the
for-each, please see that thread (many mvps weighing in).

--- N

"Jay" <j@h.com> wrote in message news:a8LSc.10$xs.0@okepread02...
When doing a foreach statement like the following why is the variable
read-only?

foreach (e in theArray)

What if I have an array of objects and I need to create a new instance of
the object. If the e is read-only then I have to use a for statement to
accomplish this.

Nov 16 '05 #4
i think a better way to put it is you can't change an element of your
enumerating through, if you have an array and you want to cycle through
it, you can make changes to the list your cycling through
Actually using *foreach* you cannot change the list per se, but you can
change the internal state of the items.
--

Stoitcho Goutsev (100) [C# MVP]

Stoitcho Goutsev (100) [C# MVP] wrote:
Jay,

It is read-only just to prevent you for making mistakes.

*e* (in your example) is nothing more than a local variable. You may change its value, but it won't affect the reference kept in the collection. Having it read only let the compiler emit an error if this happens.

e.SomeProp = ...; at the other hand is correct code. However whether it is going to chanage the object in the collection depends on what is the type of the collection (array or non-typed collection) and what is the type of the item (reference or value type).

Nov 16 '05 #5

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

Similar topics

2
2270
by: For example John Smith | last post by:
I have the following issue: I read lines of information from a log file. Each line is identified by an ID number. Due to nature of the logging process it is possible blocks are logged more than...
104
7042
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
27
79882
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
4
1405
by: Mrinal Kamboj | last post by:
Hi , I am confused that's is not even correctly mentioned in MSDN , if my code snippet works , have a look at this : 1. string s = {"1", "2","3"} ; foreach(string ss in s) { ss = "4" ; //...
29
4195
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
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,...
1
6892
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...
1
4917
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
4597
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...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.