Skip to content

Collection in c# (data structure to store the names of 5 stu

Sinu  Joseph

Sinu Joseph

Asked Jun 2, 2013, 1:38 AM EDT1 answer
Share
LinkedInX
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  Sharma
    0
    Hi sinu joseph,

    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 :)
    0