Ask a questionAsk a question
 

Proposed AnswerC# List<> question

  • Wednesday, November 04, 2009 6:08 PMCJoeBob Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Im new to the C# language and I have now advanced to lists.
    I placed a few lines of code below and here is my question:     If every needed object I make I want to keep and I use a List<>  then every time I must supply a unique object name(reference).  So if Lists were made to replace arrays when possible,  how then would C# program create unique names to create objects and then store them in a List<> at run-time?


     

    Not included in the snippet is the student class that has a method() to enter the name, ____,height etc of each student.
    After running this code of course all reference the last objetc and thus all are the same.  An array[x] by the very nature of using x to index it becomes a unique name at run time.  Below I could use s1,s2,s3 for each object but not very practical for real world.   Some one please explain how this is done in real world, please.


     

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    List<student> stdb = new List<student>();

     

               student st = new student();

     

       st.enterstudent(

    "John Miller", 44, 1, 69, 190);                            stdb.Add(st);
       st.enterstudent(
    "Robert Sanchez", 38, 1, 70, 160);                     stdb.Add(st);
       st.enterstudent(
    "Jim Goalten", 71, 1, 65, 145);                           stdb.Add(st);

     

     

    foreach(student x in stdb) student.display(x);

    }
    }
    }




All Replies

  • Wednesday, November 04, 2009 6:29 PMno12 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Your question doesen't really relate to Windows Live Messenger client development, and I don't quite understand what your asking for either.


    But anyway in C# items in lists aren't referenced by names, you use an index.

    e.g.:
    Console.WriteLine(stdb[0].whatever);
    //This would output the whavever property of the first student structure/class in the list.
    • Proposed As Answer byno12 Wednesday, November 04, 2009 6:30 PM
    •