How to set array length in c# dynamically

后端 未结 9 928
长情又很酷
长情又很酷 2020-12-30 23:45

I am still new to C# and I\'ve been struggling with various issues on arrays. I\'ve got an array of metadata objects (name value pairs) and I would like to know how to creat

相关标签:
9条回答
  • 2020-12-31 00:18

    Does is need to be an array? If you use an ArrayList or one of the other objects available in C#, you won't have this limitation to content with. Hashtable, IDictionnary, IList, etc.. all allow a dynamic number of elements.

    0 讨论(0)
  • 2020-12-31 00:19

    You can create an array dynamically in this way:

     static void Main()
        {
            // Create a string array 2 elements in length:
            int arrayLength = 2;
            Array dynamicArray = Array.CreateInstance(typeof(int), arrayLength);
            dynamicArray.SetValue(234, 0);                              //  → a[0] = 234;
            dynamicArray.SetValue(444, 1);                              //  → a[1] = 444;
            int number = (int)dynamicArray.GetValue(0);                      //  → number = a[0];
    
    
            int[] cSharpArray = (int[])dynamicArray;
            int s2 = cSharpArray[0];
    
        }
    
    0 讨论(0)
  • 2020-12-31 00:21

    Typically, arrays require constants to initialize their size. You could sweep over nvPairs once to get the length, then "dynamically" create an array using a variable for length like this.

    InputProperty[] ip = (InputProperty[])Array.CreateInstance(typeof(InputProperty), length);
    

    I wouldn't recommend it, though. Just stick with the

    List<InputProperty> ip = ...
    ...
    update.Items = ip.ToArray();
    

    solution. It's not that much less performant, and way better looking.

    0 讨论(0)
  • 2020-12-31 00:23

    Or in C# 3.0 using System.Linq you can skip the intermediate list:

    private Update BuildMetaData(MetaData[] nvPairs)
    {
            Update update = new Update();
            var ip = from nv in nvPairs
                     select new InputProperty()
                     {
                         Name = "udf:" + nv.Name,
                         Val = nv.Value
                     };
            update.Items = ip.ToArray();
            return update;
    }
    
    0 讨论(0)
  • 2020-12-31 00:27

    Use Array.CreateInstance to create an array dynamically.

        private Update BuildMetaData(MetaData[] nvPairs)
        {
            Update update = new Update();
            InputProperty[] ip = Array.CreateInstance(typeof(InputProperty), nvPairs.Count()) as InputProperty[];
            int i;
            for (i = 0; i < nvPairs.Length; i++)
            {
                if (nvPairs[i] == null) break;
                ip[i] = new InputProperty();
                ip[i].Name = "udf:" + nvPairs[i].Name;
                ip[i].Val = nvPairs[i].Value;
            }
            update.Items = ip;
            return update;
        }
    
    0 讨论(0)
  • 2020-12-31 00:30

    Use this:

     Array.Resize(ref myArr, myArr.Length + 5);
    
    0 讨论(0)
提交回复
热议问题