Difficulty in defining a list of objects in C #

I wrote a program that should get a list of names and information. Well, I have to solve this question several classes, such as students and universities have to answer. But the problem is where I plan to get the student information

static void Main(string[] args)
{
    Console.WriteLine("Some students enter the information you want? ");
    Count = int.Parse(Console.ReadLine());

    University university = new University();        
    university.Add();
    university.Sort();
    university.Report();
    Console.ReadKey();
}

      

To solve this problem, I have to indicate the number of students.

class University
{
    Student[] student = new Student[3]; 

     private int n = 0;
     public University()
     {
         for (int i = 0; i < 3; i++)
             student[i] = new Student();
     }

     public void Add()
     {
         Console.WriteLine();
         for (int i = 0; i < 3; i++)
         {
             student[n].Read();
             n++;
         }
     } 

      

For example, I have three students. How can I create a way to enter the number of students who are giving information instead of the default? Instead of the number 3, a variable that gives the user the desired number.

+3


source to share


2 answers


You can define a property that would hold the number of students who attend your university and initialize it while you create a university.

class University
{
    public int NumbersOfStudents { get; private set; }

    public Student[] students;

    public University(int numberOfStudents)
    {
        NumberOfStudents = numberOfStudents;
        students = new Student[numberOfStudents];
    }
}

      

So, if you want to create a university with 1000 students, you would do it like this:

var university = new University(1000);

      

Now the named object university

has an array of students with 1000 slots. In fact, every element in this array null

. I mean

university.Students[0], university.Students[1], ...

      

are null

.



However , I don’t think this is the most efficient way to get what you want. If I were you, I would choose List

for this purpose. We usually use an array when we know the number of elements that we will be placing there, and in the future we will meet any case when we want to add another element, this is not possible using an array because the array has a fixed size. On the other hand, it List

is a data structure with no fixed size, it can expand as you want to add more elements to it.

In terms of code, I would choose the following:

class University
{
    public List<Student> Students {get; private set; }

    public University()
    {
        Students = new List<Student>();
    }
}

      

Now you can instantiate the university and do this, you will also create a new student list with no items.

var university = new University();

      

Now you can add a student to a university student as simple as the following:

university.Students.Add(new Student());

      

+2


source


I would refactor your code a bit to use lists, in which case lists are more powerful than arrays because arrays are not mutable.



static void Main(string[] args) {
 Console.WriteLine("Please enter the number of students: ");
 Count = int.Parse(Console.ReadLine());
 University university = new University(Count);
 university.AddStudent(new Student() /*Student logic here.*/);
 university.Sort();
 university.Report();
}

class Student {
 //Properties here...
 public Student(){
      //Default values here...
 }
}

class University {
 private List<Student> _Students;
 public Students {
      get {
           return _Students;
      }
      //In Microsoft guidelines, list shouldn't be exposed as properties.
      protected set {
           //Add list replacement logic here.
           _Students = value;
      }
 }
 public University(Int32 StudentCount) {
      _Students = new List<Student>(new Student[StudentCount]);
 }
 public void AddStudent(Student StudentToAdd) {
      //Add your logic here...
      _Students.Add(StudentToAdd);
 }
}

      

+1


source







All Articles