简体   繁体   中英

Making roster program, having issues with creating the arrays, C#

In c#

I am trying to make a simple program that will have a few functions such as adding and deleting members from a roster, search for a member, and display the current # of members. This will later be built upon for more options, and saving the members to a file so it can actually be used over time. For now I just wanted the general layout setup before I even bothered with learning how to save the arrays to a file.

Now my question is: When the user chooses to create a new member for the roster it sends it to a function that will create a new employee with an array that will store Name and Division (just basic info for the test program). How would I create a multidimensional array without declaring how many rows it would need? I can not guess the amount of members due to eventual growth and reduction.

tl:dr Creating roster test program. How do I create a multidimensional array that would have a function add another row to it when a new member joins?

Arrays are not resizable you would want to use List<List<T>> where T is the type of value you wish to store.

However you don't necessarily want to be storing the properties in an array, you really want to use a class instead.

public class Employee {
  public string Name {get;set;}
  public string Division {get;set;}
}

And then have a list of Employee

var roster = new List<Employee>();

To add a new Employee

roster.Add(new Employee { Name = "Mr Tiddles", Division = "Feline" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM