473,626 Members | 3,191 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 1043
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$x s.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$x s.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
2280
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 once, but in these cases the IDs will be similar. In my PHP script I temporarily store all read ID numbers in an array. When done reading I want to sort the array and then loop through it to check for doubles. Right now all I am interested in is...
104
7137
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
27
79909
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
1416
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" ; // Compilation error as foreach loop is Read Only as
29
4221
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 a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.