Collection in c# (data structure to store the names of 5 stu
craete a data structure to store the names of 5 students.this datastructure should allow only strings to stored in it
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Sunny SharmaRank #223 · 11.1K pointsPosted Jun 2, 2013, 10:15 PM EDT
You can do it like this:
---------------------------------
public struct Students
{
public string[] Names; //In C# you can not initialize variables inside struct.
public Students(int NoOfStudents)
{
Names = new string[NoOfStudents]; //here you initialize the string array of struct.
}
}
--------------------------------
Below is how you use it:
Students stdList = new Students[5]; // it will initialize the Names array for 5 names.
stdList.Names[0]="Name1";
stdList.Names[1]="Name2";
stdList.Names[2]="Name3";
stdList.Names[3]="Name4";
stdList.Names[4]="Name5";
foreach(string str in stdList.Names)
{
Console.WriteLine(str); // Print the name to screen.
}
Hope it helps :)