You can't do what you're asking - at least directly.
You could start with simply putting the strings into an array and then work from the array.
string[] strings = new []
{
string_1,
string_2,
string_3,
string_4,
};
for(int i = 0; i < 4; i++)
{
strings[i] = "Hello, world! " + i;
}
Console.WriteLine(string_3); // != "Hello, world! 2"
Console.WriteLine(strings[2]); // == "Hello, world! 2"
But then the original string_3
is unchanged, although its slot in the array is correct.
You can go one step further and do it this way:
Action<string>[] setStrings = new Action<string>[]
{
t => string_1 = t,
t => string_2 = t,
t => string_3 = t,
t => string_4 = t,
};
for(int i = 0; i < 4; i++)
{
setStrings[i]("Hello, world! " + i);
}
Console.WriteLine(string_3); // == "Hello, world! 2"
This works as originally intended - string_3
does get updated. It is a little contrived though, but may be useful to you.