473,387 Members | 1,489 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,387 software developers and data experts.

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 1035
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.