Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# reference to loop variable

Tags:

c#

reference

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?

like image 252
Dasha Salo Avatar asked Jul 03 '09 07:07

Dasha Salo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

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);
}
like image 94
Marc Gravell Avatar answered Nov 11 '22 01:11

Marc Gravell


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
like image 42
Jørn Schou-Rode Avatar answered Nov 11 '22 03:11

Jørn Schou-Rode


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;
}
like image 2
Talljoe Avatar answered Nov 11 '22 02:11

Talljoe