gpt4 book ai didi

c# - 返回包含两个数组的元组

转载 作者:太空狗 更新时间:2023-10-30 00:22:43 24 4
gpt4 key购买 nike

我正在尝试调用一个返回包含两个数组的元组的函数。数组的内容基于 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’转换为非委托(delegate)类型‘Tuple’”。

这是我第一次使用元组,如何在不调用函数两次的情况下返回两个数组?

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

最佳答案

改变

return Tuple.Create<allowed, notallowed>;

return Tuple.Create(allowed, notallowed);

第一个语法用于泛型:<

第二个用于方法调用:(

关于c# - 返回包含两个数组的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55418599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com