Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为C#泛型集合获取一致的.Count/.Length属性?_C#_Generics_Collections - Fatal编程技术网

如何为C#泛型集合获取一致的.Count/.Length属性?

如何为C#泛型集合获取一致的.Count/.Length属性?,c#,generics,collections,C#,Generics,Collections,List具有.Count属性,其中asTarrays.Length。我认为这是因为数组是固定长度的,而其他数组不是,但语法上的差异仍然令人沮丧 如果从数组重构为列表,则会出现“不包含.Length的定义”错误,而且当.Count和.Length基本相同时,必须更改它似乎是浪费时间 有没有一个好办法来解决这个问题?是否可以扩展列表以添加一个.Length属性,例如,该属性是.Count的别名,对于泛型数组也是如此?出于任何原因,这是个坏主意吗?您可以将数组放入IList类型的变量中。(阵列实现此接

List
具有
.Count
属性,其中as
T
arrays
.Length
。我认为这是因为数组是固定长度的,而其他数组不是,但语法上的差异仍然令人沮丧

如果从数组重构为列表,则会出现“不包含.Length的定义”错误,而且当
.Count
.Length
基本相同时,必须更改它似乎是浪费时间


有没有一个好办法来解决这个问题?是否可以扩展
列表
以添加一个
.Length
属性,例如,该属性是
.Count
的别名,对于泛型数组也是如此?出于任何原因,这是个坏主意吗?

您可以将数组放入
IList
类型的变量中。(阵列实现此接口)

然后,您可以完全按照使用任何其他
IList
的方式使用数组(尽管
Add
Remove
会抛出异常,因为数组是固定长度的)

您可以使用LINQ提供的方法

在可能的情况下(或者在.NET 4中也使用非通用接口),可以优化该接口以使用接口提供的属性。因此,阵列、
列表
等都将得到优化

var yourList = new List<string> { "the", "quick", "brown", "fox" };
int count1 = yourList.Count();  // uses the ICollection<T>.Count property

var yourArray = new[] { 1, 2, 4, 8, 16, 32, 64, 128 };
int count2 = yourArray.Count();  // uses the ICollection<T>.Count property

var yourEnumerable = yourArray.Where(x => x > 42);
int count3 = yourEnumerable.Count();  // no optimisation, iterates the sequence
var yourList=新列表{“the”、“quick”、“brown”、“fox”};
int count1=yourList.Count();//使用ICollection.Count属性
var yourray=new[]{1,2,4,8,16,32,64,128};
int count2=yourArray.Count();//使用ICollection.Count属性
var yourEnumerable=yourArray.Where(x=>x>42);
int count3=yourenumable.Count();//无优化,重复序列
或者,如果您想要某种类型的一致计数属性,而不需要在非优化情况下迭代整个序列,那么您可以创建自己的扩展方法。(我个人不会走这条路。)

int count4=yourList.GetCount();//使用ICollection.Count属性
int count5=yourArray.GetCount();//使用ICollection.Count属性
int count6=yourenumable.GetCount();//编译时错误
// ...
公共静态类集合扩展
{
公共静态int GetCount(此ICollection源)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
返回source.Count;
}
公共静态int GetCount(此ICollection源)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
返回source.Count;
}
}

您可以简单地扩展泛型列表并添加计数方法,但是,如果原因仅仅是因为您重新分解了因子,并且不想更新为计数,我不建议这样做。如果没有真正添加任何内容,则无需扩展类。为什么不更新代码以使用Count而不是Length?

您可以对列表和数组使用
.Count()
方法


.Count()
方法在可用时运行,然后返回到传统的
.Length

以及实现非泛型
ICollection
的任何集合。需要注意的是,数组实现了这两个接口,因此它们从快捷逻辑中获益。在.NET4之前,这难道没有性能问题吗?我认为它是循环的(即使没有谓词),因此可能会慢很多。只需在谷歌上做一个“linq count slow”就可以给出很多例子。听起来它将是.NET4的解决方案,尽管有了优化。@mikel:我不知道。直到.NET 4,它才对非泛型的
ICollection
进行优化,但我认为对于一个集合来说,实现泛型的
IEnumerable
,非泛型的
ICollection
,而不是泛型的
ICollection
,是相对不寻常的,这是新的
i收集
优化在标准
i收集
优化不起作用的情况下生效所需的条件。(当然,
Count
方法总是比直接使用任何基础的
Count
属性稍微慢一些,但是在日常使用中您不会注意到。)但是OP询问为什么
列表
不包含
Length
属性
Count
执行相同的操作,但
Length
的时间完整性为O(1)
int count4 = yourList.GetCount();  // uses the ICollection<T>.Count property
int count5 = yourArray.GetCount();  // uses the ICollection<T>.Count property
int count6 = yourEnumerable.GetCount();  // compile-time error

// ...

public static class CollectionExtensions
{
    public static int GetCount<T>(this ICollection<T> source)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.Count;
    }

    public static int GetCount(this ICollection source)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.Count;
    }
}