C# 接受;“参数”;哪些是清单本身?
我正在尝试为一个项目编写一个方法,该方法将任意数量的列表作为参数,并返回一个包含所有这些列表共享的术语的新列表。我有函数代码,但我更喜欢使用params关键字,而不是创建一个列表列表,其中包含我想要比较的所有列表C# 接受;“参数”;哪些是清单本身?,c#,list,params,C#,List,Params,我正在尝试为一个项目编写一个方法,该方法将任意数量的列表作为参数,并返回一个包含所有这些列表共享的术语的新列表。我有函数代码,但我更喜欢使用params关键字,而不是创建一个列表列表,其中包含我想要比较的所有列表 static List<T> Shared<T>(List<T> first, List<T> second) { List<T> result = new List<T>(); foreach (
static List<T> Shared<T>(List<T> first, List<T> second)
{
List<T> result = new List<T>();
foreach (T item in first)
if (second.Contains(item) && !result.Contains(item)) result.Add(item);
return result;
}
static List<T> Shared<T>(List<List<T>> lists)
{
List<T> result = lists.First();
foreach (List<T> list in lists.Skip(1))
{
result = Shared<T>(result, list);
}
return result;
}
静态列表共享(列表第一,列表第二)
{
列表结果=新列表();
foreach(第一个中的T项)
if(second.Contains(item)&&!result.Contains(item))result.Add(item);
返回结果;
}
静态列表共享(列表)
{
列表结果=lists.First();
foreach(列表中的列表。跳过(1))
{
结果=共享(结果、列表);
}
返回结果;
}
是我当前的代码,比较两个列表效果很好,但是为了比较两个以上的列表,我必须创建一个新列表,如下所示:
List<int> nums1 = new List<int> { 1, 2, 3, 4, 5, 6 };
List<int> nums2 = new List<int> { 1, 2, 3 };
List<int> nums3 = new List<int> { 6, 5, 3, 2 };
List<int> listOfLists = Shared<int>(new List<List<int>> {nums1, nums2, nums3});
foreach (int item in listOfLists)
Console.WriteLine(item);
//Writes 2 and 3
List nums1=新列表{1,2,3,4,5,6};
List nums2=新列表{1,2,3};
List nums3=新列表{6,5,3,2};
List listOfLists=共享(新列表{nums1,nums2,nums3});
foreach(列表中的int项)
控制台写入线(项目);
//写2和3
我真的希望能够使用Shared(list1,list2,list3,list4…),即使这段代码已经有些功能了。目前,任何使用params版本的尝试都会抱怨“方法‘Shared’没有重载包含N个参数”
我也知道我的代码可能会更有效率,所以我也很高兴看到关于这方面的建议,但首先我需要弄清楚为什么使用params不起作用——如果可能的话。你在找这个吗
static List<T> Shared<T>(params List<T>[] lists)
静态列表共享(参数列表[]列表)
params
参数必须始终具有数组类型,但它可以是列表数组。是否正在查找此参数
static List<T> Shared<T>(params List<T>[] lists)
静态列表共享(参数列表[]列表)
params
参数必须始终具有数组类型,但它可以是列表数组。可以轻松完成:
using System.Linq;
// ..
static List<T> Shared<T>(params List<T>[] lists)
{
if (lists == null)
{
throw new ArgumentNullException("lists");
}
return Shared(lists.ToList());
}
使用System.Linq;
// ..
静态列表共享(参数列表[]列表)
{
if(lists==null)
{
抛出新的异常(“列表”);
}
返回共享列表(lists.ToList());
}
可以轻松完成:
using System.Linq;
// ..
static List<T> Shared<T>(params List<T>[] lists)
{
if (lists == null)
{
throw new ArgumentNullException("lists");
}
return Shared(lists.ToList());
}
使用System.Linq;
// ..
静态列表共享(参数列表[]列表)
{
if(lists==null)
{
抛出新的异常(“列表”);
}
返回共享列表(lists.ToList());
}
基于@Selman22提出方法签名的响应,您也可以使用此LINQ查询来实现所需的结果
static List<T> Shared<T>(params List<T>[] lists)
{
return
lists.Skip(1).Aggregate( // Skip first array item, because we use it as a seed anyway
lists.FirstOrDefault(), // Seed the accumulator with first item in the array
(accumulator, currentItem) => accumulator.Intersect(currentItem).ToList()); // Intersect each item with the previous results
}
基于@Selman22提出方法签名的响应,您也可以使用此LINQ查询来实现所需的结果
static List<T> Shared<T>(params List<T>[] lists)
{
return
lists.Skip(1).Aggregate( // Skip first array item, because we use it as a seed anyway
lists.FirstOrDefault(), // Seed the accumulator with first item in the array
(accumulator, currentItem) => accumulator.Intersect(currentItem).ToList()); // Intersect each item with the previous results
}
为什么你不只是用第一种方法来代替呢?@Damien_不信者我猜这不是联合,这就是为什么你不只是用第一种方法来代替吗?@Damien_不信者我猜这不是联合,我现在觉得非常愚蠢。非常感谢:)很明显,当它到达时,它仍然计划改变(请参见
Params IEnumerable
)好吧,我现在感到难以置信的愚蠢。非常感谢:)显然,我们仍计划在收到时对此进行更改(请参见Params IEnumerable
)