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
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted Jun 3, 2013, 2:15 AM
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 :)