首页 > 解决方案 > 返回具有两个数组的元组

问题描述

我正在尝试调用一个函数,该函数返回一个包含两个数组的元组。数组的内容基于checkedListBox 中的选中项。我定义数组并调用函数“storeParametersInArrays”,如下所示。

string[] allowedObjects = new string[checkedListObjects.CheckedItems.Count]; // All allowed objects
string[] notallowedObjects = new string[checkedListObjects.Items.Count - checkedListObjects.CheckedItems.Count]; // All not allowed objects

Tuple<string[], string[]> ObjParameters = storeParametersInArrays(notallowedObjects, allowedObjects, checkedListObjects);
allowedObjects = ObjParameters.Item1;
notallowedObjects = ObjParameters.Item2;

调用的函数定义为:

private Tuple<string[], string[]> storeParametersInArrays(string[] notallowed, string[] allowed, CheckedListBox checkedListBox)
{
    int i = 0; // allowed objects
    int j = 0; // not allowed objects
    int k = 0; // item counter

    foreach (object item in checkedListBox.Items)
    {
        if (!checkedListBox.CheckedItems.Contains(item))
        {
            notallowed[j++] = checkedListBox.Items[k].ToString();
        }
        else
        {
            allowed[i++] = checkedListBox.Items[k].ToString();
        }
        k++;
    }
    return Tuple.Create<allowed, notallowed>;
}

我无法返回上述代码示例中的元组。我收到错误“无法将方法组 'Create' 转换为非委托类型 'Tuple'”。

这是我第一次使用元组,我怎样才能返回两个数组而不必调用该函数两次?

我已经看过稍微类似的问题,所以如果问题已经在其他地方得到回答,我会很高兴指出正确的方向。

标签: c#tuples

解决方案


只是改变

return Tuple.Create<allowed, notallowed>;

return Tuple.Create(allowed, notallowed);

第一种语法用于泛型:<

第二个用于方法调用:(


推荐阅读