In an ideal world, you never number variables and just use an array (or some other collection). So you would have:
for (int i=0;i<5;i++)
{
num[i] = i;
}
Which requires your list of variables to be instead one variable that is indexable (like an array)
int[] num = new int[5];
The only way to access a variable in C# from a string is with reflection (which is a fairly advanced concept, easy to get wrong, and not very efficient). That being said, you'd do something like:
Type thisClass = this.GetType(); //Even better, typeof(WhateverClassThisIs);
for (int i=1;i<5;i++)
{
FieldInfo field = thisClass.GetField($"num{i}");
field.SetValue(this, i);
}
Note that this only works if num1
etc. are existing class members (it does not create them) and not local to the function. If you have local variables, you are basically out of luck.