469,636 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

C# Question: Iteration variable in a foreach() loop not always read only?


I am a pretty new programmer, so I apologize in andvance if this is a dumb question...

In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of the iteration variable is used and you cannot modify it. For example:
Expand|Select|Wrap|Line Numbers
  1. int[] pins = {9, 3, 7, 2}
  2. int newPin = 10;
  3. foreach(int pin in pins)
  4. {
  5.     pin = newPin;  //Will give an error because pin is a read-only copy...
  6. }
That makes sense to me, I can live with that. However, recently when I was working with a DataRow object, I found what appeared to be a contradiction to that rule. When I used a DataRow variable as an iteration variable, I was able to update a value in one of the columns in the DataRow. For example:
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = new DataTable();
  2. dt.Columns.Add("ID");
  3. dt.Columns.Add("Name");
  4. dt.Columns.Add("Msg");
  5. //code to fill dt
  6. foreach(DataRow dr in dt.Rows)
  7. {
  8.     string evaluationVariable, errorMsg;
  9.     //Code to populate the evaluationVariable and errorMsg
  10.     if (evaluationVariable == "Error")
  11.     {
  12.         dr["Msg"] = errorMsg;  //No error...but isn't dr["Msg"] read-only?
  13.     }
  14.     else
  15.     {
  16.         dr["Msg"] = "";  //No error...but isn't dr["Msg"] read-only?
  17.     }
  18. }
I'm sorry for the crudeness of the examples above, but I'm hoping that they get my point across. If not, let me know and I'll try to clarify.

Are there certain times, like when working with a DataRow that the iteration variable in a foreach() loop is not read only or am I missing something (a very real possibility)? It would seem that there are two different rules for a foreach() loop's iteration variable. I have looked and looked on Google for the answer to this and I have not been able to find an answer telling me why I am able to update a value in the DataRow column which is the iteration value in a foreach() loop. Could somebody please help out a beginner and show me what I'm missing?

Thanks for any help that you can give.

Sep 28 '08 #1
1 3088
Shashi Sadasivan
1,435 Expert 1GB
Well, i found that out while i was programming, and have an explanation of that from my understanding.
However can you quote the exact words of that statement from the book ?

when you do a for loop over int or primitive data, a copy of the data is made only for the for loop element, and the compiler is smart enough to stop compiling, to alert that the element will not change the same element in the array.

However with the datarow example, the looping element is a reference of the array element. and any change made to the element will reflect in the array element as they both point to the same location.

Hope this makes some sense.
Sep 28 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by siliconwafer | last post: by
15 posts views Thread by MuZZy | last post: by
9 posts views Thread by news.microsoft.com | last post: by
11 posts views Thread by SM | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.