How can I create a 2 dimensional dynamic length array?

后端 未结 3 1250
青春惊慌失措
青春惊慌失措 2021-01-05 05:44

I want to create a 2-dimensional array, without knowing the size of the first dimension.

For example I have an unknown number of rows, when I create an array. Ever

相关标签:
3条回答
  • 2021-01-05 06:25

    As long as I know, we cant instantiate array without knowing its size. Why dont you try a Array of List? Like this:

    List<int>[] a = new List<int>[yourDesireColumnNumber];
    

    With List, add, select, input elements is trivial. If you want to give it as parameter in other functions, just define Type.

    0 讨论(0)
  • 2021-01-05 06:42

    There is no such thing as dynamic length arrays in .NET. Use a List<> instead.

    The array bounds all need to be known when you instantiate an array. What may have confused you is that this seems to be different for jagged arrays, but it's not: since it is an array of arrays, when you instantiate it it will be an array of uninstantiated arrays (e.g. null references). You then need to allocate each of those arrays again to use them.

    0 讨论(0)
  • 2021-01-05 06:45

    IMO, since the "columns" are fixed, declare a class for that:

    public class Account
    {
        public int ID {get;set;}
        public string Name {get;set;}
        public string User {get;set;}
        public string Password {get;set;} // you meant salted hash, right? ;p
    }
    

    now have a :

    List<Account> list = new List<Account>();
    

    this has everything you need:

    add to list,select,input elements

    list.Add etc

    using elements in database queries using as parameters in other functions

    vague without more info, but you can pass either the Account or invidual values, or the entire list.

    0 讨论(0)
提交回复
热议问题