tags:

views:

385

answers:

5

Hello,

Is it possible in C# to something like the following

foreach (ref string var in arr) {
    var = "new value";
}

so that var variable was treated as reference and assigning to var would change an array element?

+16  A: 

There is no such construct for updating a loop; an iterator is read-only. For example, the following provides a perfectly valid iterator:

public IEnumerable<int> Get1Thru5() {
    yield return 1; yield return 2; yield return 3;
    yield return 4; yield return 5;
}

How would it update? What would it update?

If the data is an array/list/etc, then something like:

for(int i = 0 ; i < arr.Length ; i++) {
    arr[i] = "new value";
}

Or other options depending on the specific container.


Update; at a push, an extension method:

public static void UpdateAll<T>(this IList<T> list, Func<T, T> operation) {
    for (int i = 0; i < list.Count; i++) {
        list[i] = operation(list[i]);
    }
}
static void Main() {
    string[] arr = { "abc", "def", "ghi" };
    arr.UpdateAll(s => "new value");
    foreach (string s in arr) Console.WriteLine(s);
}
Marc Gravell
Very good example with the Ger1Thru5 and well explained. +1
DoctaJonez
+2  A: 

No. The foreach statement is simply syntax sugar on top of the IEnumerable interface. This interface defines a method to get en IEnumerator which in turn has methods to do read-only enumeration:

  • Current : object
  • MoveNext() : bool
  • Reset() : void
Jørn Schou-Rode
*strictly* speaking it doesn't even need IEnumerable/IEnumerator, and it never uses Reset() - but indeed (+1)
Marc Gravell
And if you're using an array, it doesn't use IEnumerable at all.
Jon Skeet
A: 

In the case of a string, no; C# strings are immutable (cannot be changed). If you were enumerating over objects of a different, mutable type, you can change the properties of those objects.

Jacob
+2  A: 
foreach(string s in strings)
{
  Console.WriteLine(s);
}

is compiler shortcut for:

IEnumerator e = strings.GetEnumerator();
string s;
while(e.MoveNext())
{
  s = e.Current;
  Console.WriteLine(s);
}

Since IEnumerator.Current is a get-only property you can't set the value.

// Non-generic IEnumerator shown.
interface IEnumerator
{
  bool MoveNext();
  object Current { get; }
  void Reset();
}

If you want to support an updatable enumerator you will need to create it yourself -- but you won't be able to use "foreach" with it, and you'll have to implement wrappers around all the common IEnumerable classes.

You'll have to analyze your current situation and figure out how to update. If you're using an IList interface you can do:

for(int i = 0; i < strings.Count; ++i)
{
  string s = strings[i];
  //do work
  s = s.ToUpperInvariant();
  strings[i] = s;
}
Talljoe
Actually, the "string s" is **outside** the loop; this is important to understand the infamous foreach/capture issue. And it will use `Dispose()` if IDisposable is implemented
Marc Gravell
Was more for demonstration than accuracy, but you're right, @Marc Gravell. I've moved the string outsid eof the loop. Leaving Dispose() as an exercise to the reader as it's orthoganol to this discussion.
Talljoe
+1  A: 

In the case of a string, no; C# strings are immutable (cannot be changed). If you were enumerating over objects of a different, mutable type, you can change the properties of those objects.

Just to illustrate what Jacob is talking about. Consider the code snippet below:

class MyInt
{
    public int Val { get; set; }
    public MyInt(int val) { this.Val = val; }
}

class Program
{
    static void Main(string[] args)
    {
        MyInt[] array = new MyInt[] { new MyInt(1), new MyInt(2) };

        foreach (var obj in array) Console.Write("{0}\t", obj.Val);

        foreach (var obj in array)
        { 
            obj = new MyInt(100); // This doesn't compile! the reference is read only
            obj.Val *= 10; // This works just fine!
        }

        foreach (var obj in array) Console.Write("{0}\t", obj.Val);
    }
}

Indeed, if you try to assign to the "obj" as above you'll get a compile time error. But nothing prevents you from modifying MyInt's properties through the "obj" reference

Dmitry O_o