如何设置数组长度在C#动态 [英] How to set array length in c# dynamically

查看:239
本文介绍了如何设置数组长度在C#动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是新的C#和我一直在与阵列的各种问题挣扎。我有元数据对象的数组(名称值对),我想知道如何创建只有我真正需要的InputProperty对象的数量。在这个循环中,我随意设定元素20的号码,我试图摆脱困境的时候条目变得无效,但在此接收端的web服务不喜欢传递给它的任何空元素:

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 create only the number of "InputProperty" objects that I truly need. In this loop I've arbitrarily set the number of elements to 20 and I try to bail out when the entry becomes null but the web service on the receiving end of this does not like any null elements passed to it:

private Update BuildMetaData(MetaData[] nvPairs)
{
	Update update = new Update();
	InputProperty[] ip = new InputProperty[20];  // how to make this "dynamic"
	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;
}

总之,说我只有3 namevalue对上述输入数组中?而不是分配给数组称为IP 20个元素,怎能code这使IP仅仅是大,因为它必须这样做。更新对象跨越另一个web服务传递,因此序列化是很重要的(即我不能使用NameValueCollection中,等)。

In summary, say I only have 3 namevalue pairs in the above input array? Rather than allocate 20 elements for the array called ip, how can code this so ip is only as big as it needs to be. The update object is passed across another webservice so serialization is important (i.e. I can't use namevaluecollection, etc.).

P.S。是通过添加注释设施上贴出的问题随动的唯一途径?

p.s. Is the only way to followup on a posted question through the "add comments" facility?

推荐答案

如果你不想使用列表的ArrayList 或其它动态调整大小的集合,然后转换成数组(这是我推荐的方法,方式),那么你就必须分配阵列的最大可能大小,保持跟踪你有多少项目放在它,然后创建只有在它的项目一个新的数组:

If you don't want to use a List, ArrayList, or other dynamically-sized collection and then convert to an array (that's the method I'd recommend, by the way), then you'll have to allocate the array to its maximum possible size, keep track of how many items you put in it, and then create a new array with just those items in it:

private Update BuildMetaData(MetaData[] nvPairs)
{
    Update update = new Update();
    InputProperty[] ip = new InputProperty[20];  // how to make this "dynamic"
    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;
    }
    if (i < nvPairs.Length)
    {
        // Create new, smaller, array to hold the items we processed.
        update.Items = new InputProperty[i];
        Array.Copy(ip, update.Items, i);
    }
    else
    {
        update.Items = ip;
    }
    return update;
}

另一种方法是将始终分配 update.Items = IP; ,如有必要然后调整:

update.Items = ip;
if (i < nvPairs.Length)
{
    Array.Resize(update.Items, i);
}

这是少code,但可能最终会做相同的工作数量(即创建一个新的阵列和复制旧的项目)。

It's less code, but will likely end up doing the same amount of work (i.e. creating a new array and copying the old items).

这篇关于如何设置数组长度在C#动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆