Foreach vs for loop in C#. Creation of new object is possible in for loop, but not possible in foreach loop -


i have wonder why can create new object of class 'someclass' in for loop, can't same in foreach loop.

the example bellow:

someclass[] n = new someclass[10];  foreach (someclass in n)  {    = new someclass(); // cannot assign 'i' because 'foreach iteration variable' }  (int = 0; < n.length; i++)  {    n[i] = new someclass(); // ok } 

can explain me scenario?

foreach iteration loops known 'read-only contexts.' cannot assign variable in read-only context.

for more info: http://msdn.microsoft.com/en-us/library/369xac69.aspx


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -