c# array.copy_使用Array.Contains()C#2.0时的技巧

c# array.copy

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this?

如果您可以像List对象上可用的那样使用Contains方法测试数组中是否包含元素,那不是很好吗? 如果您可以编写这样的代码,那不是很好吗?

string[] stuff = ....;
if (stuff.Contains(“item”))
{
    ...
}

In .NET 3.5, this is possible out of the box (make sure you reference System.Core and include the System.Linq namespace) but if you try to run this code in .NET 2.0 or .NET 3.0, you will get errors. Nevertheless the .NET Framework 2.0 does provide a Contains() method on any Array object.

在.NET 3.5中,这可能是开箱即用的(请确保引用System.Core并包括System.Linq命名空间),但是如果尝试在.NET 2.0或.NET 3.0中运行此代码,则会出现错误。 但是,.NET Framework 2.0确实在任何Array对象上提供了Contains()方法。

In the .NET Framework 2.0, System.Array implements the System.Collections.Generic.IList<T> interface. Unfortunately the implementation of the IList interface is provided at runtime, so we do not see the methods in Visual Studio and we cannot write array.Contains().

在.NET Framework 2.0中,System.Array实现System.Collections.Generic .IList <T>接口。 不幸的是,IList接口的实现是在运行时提供的,因此我们在Visual Studio中看不到这些方法,也无法编写array.Contains()。

Instead, we have to cast the array to the appropriate IList interface:

相反,我们必须将数组强制转换为适当的IList接口:

string[] arr = new string[] { “RR US”, “RR India”, “RR UK” };
if (!((IList<string>)arr).Contains(“India”))
{
     System.Console.WriteLine ("Correct! We are working with RR India");
}

The documentation explains it as follows:

该文档对它的解释如下:

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

在.NET Framework 2.0版中,Array类实现System.Collections.Generic .IList,System.Collections.Generic .ICollecti on和System.Collections.Generic .IEnumerab 通用接口。 这些实现是在运行时提供给数组的,因此对于文档构建工具不可见。 结果,通用接口不会出现在Array类的声明语法中,并且没有接口成员的参考主题,这些参考成员只能通过将数组转换为通用接口类型来访问(显式接口实现)。 将数组强制转换为这些接口之一时要注意的关键是,添加,插入或删除元素的成员会抛出NotSupportedException。

So next time, you can save yourself from writing (unnecessary) code like this:

因此,下次,您可以避免编写这样的(不必要的)代码:

bool found =false;
    foreach (string s in array)
    {
        if (s.Equals(“item”))
        {
            found =true;
            break;
        }
    }
    if (found)
    {
       ........
    }

Some of you guys might have an alternate solution for this.

你们中有些人可能对此有替代解决方案。

For example

例如

bool exists = Array.IndexOf(arr, "RR India") >= 0

But, this is for c# 2.0 and as explained above .NET Framework 2.0 does provide a Contains() method on any Array object. So this may helpful for framework wise comparison.

但是,这是针对c#2.0的 ,如上所述,.NET Framework 2.0确实在任何Array对象上提供了Contains()方法。 因此,这可能有助于进行框架明智的比较。

Happy Programming!!!

编程愉快!

翻译自: https://www.experts-exchange.com/articles/7412/Trick-when-using-Array-Contains-C-2-0.html

c# array.copy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值