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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greyseal96
    New Member
    • Sep 2008
    • 1

    #1

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

    Hi,

    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:
    Code:
    int[] pins = {9, 3, 7, 2}
    int newPin = 10;
    foreach(int pin in pins)
    {
        pin = newPin;  //Will give an error because pin is a read-only copy...
    }
    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:
    Code:
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    dt.Columns.Add("Msg");
    //code to fill dt
    foreach(DataRow dr in dt.Rows)
    {
        string evaluationVariable, errorMsg;
        //Code to populate the evaluationVariable and errorMsg
        if (evaluationVariable == "Error")
        {
            dr["Msg"] = errorMsg;  //No error...but isn't dr["Msg"] read-only?
        }
        else
        {
            dr["Msg"] = "";  //No error...but isn't dr["Msg"] read-only?
        }
    }
    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.

    Regards,
    John
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    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.

    Comment

    Working...