I have a class Student with properties sub1,sub2, and sub3.
I want to access those properties using a loop by concatenating the property name with an index in order to avoid duplication. I tried below code
public class SampleApplication
{
public static void Main(string[] args)
{
Student s =new Student();
for(int i=1;i<=3;i++)
{
s.$"sub{i}"="Subjects";
}
}
}
public class Student
{
public string sub1;
public string sub2;
public string sub3;
}
But I am getting an error like the identifier expected. Can anyone help me to solve this? Thanks in advance.