Issue foreach loop

M

Mrinal Kamboj

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
// it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level , so //
compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .

thanks ,

Mrinal
 
C

# Cyrille37 #

Mrinal said:
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 // it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level ,
so // compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .

Perhaps in the first example, ss is a Value,
than in second example, dr is an Object.

cyrille
 
J

Jon Skeet [C# MVP]

Mrinal said:
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
// it just associates an Enumerator .
}

Now above example is perfect and that's what MSDN says .

Now try this for any simple DataSet (ds) retrieved from a db .

foreach(DataRow dr in ds.Tables[0].Rows)
{
dr[0] = "Something" ; // Assume string column
dr.AcceptChanges() ; // it works fine at every level , so //
compilation error , no runtime issue
// whcih logically shouldn't be an issue
}

any pointers to help me out on this , can it be a MS issue , i am not
even sure about the reason for differentiating behaviour .

There's no different behaviour there. In both cases, the variable
itself is readonly. However, that doesn't stop you from accessing
properties etc of the object that the variable refers to.

If you tried:

dr = ds.Tables[0].NewRow();

then you would be changing the value of the variable, and that would be
disallowed.

It's very important to distinguish between a variable, its value, and
an object which a variable's value may be a reference to.

Jon
 
M

Marc Gravell

There is no difference in behaviour (and in both cases "ss" and "dr" are
reference-type objects); you are doing very different things and getting the
different behaviour:

in the first example you are *reassigning the variable* "ss" - i.e. ss =
in the second example you are manipulating properties etc of dr *without*
reassigning it

If you typed "dr = null;" inside the foreach, that would fail for the same
reason.

Note also that strings are immutable, so pretty-much any operation is going
to be assignment; ss+="!"; would fail for the same reason, as this doesn't
read "append exclamation to ss", but rather "build a new string by
concatenating ss and exclamation, and assign this new string to ss".

Does that make sense?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top