开发者

Object array dynamic

开发者 https://www.devze.com 2023-01-10 05:05 出处:网络
I have the following line in my code: object[] inputs = new object[] {\"input1\", \"input2\", \"input3\", \"input4\"};

I have the following line in my code:

object[] inputs = new object[] {"input1", "input2", "input3", "input4"};

I would like to know how (without knowing how many elements will be in the array) add dynamically using a loop like this:

object[] inputs;
foreach (string key in Request.Form.Keys)
{
开发者_如何学编程     inputs[0] = key;
}

How could I do this?

Thanks in advance.

Best Regards.


Could you just not use:

List<object> list = new List<object>();
list.Add(key);


I think you want something like the IEnumerable.ToArray function.

object[] inputs = Request.Form.Keys.ToArray()


use List<T> it has same access efficiency as array (O(1)) and have method Add to add elements. Read more here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消