How to set array length in c# dynamically

后端 未结 9 962
长情又很酷
长情又很酷 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:38

    You could use List inside the method and transform it to an array at the end. But i think if we talk about an max-value of 20, your code is faster.

        private Update BuildMetaData(MetaData[] nvPairs)
        {
            Update update = new Update();
            List ip = new List();
            for (int 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.ToArray();
            return update;
        }
    

提交回复
热议问题