相关疑难解决方法(0)

在C#中比较数组

我试图将两个数组相互比较.我试过这段代码并得到以下错误.

static bool ArraysEqual(Array a1, Array a2)
{
    if (a1 == a2)
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    IList list1 = a1, list2 = a2; //error CS0305: Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments
    for (int i = 0; i < a1.Length; i++)
    {
        if (!Object.Equals(list1[i], list2[i])) //error CS0021: Cannot apply indexing with [] to an expression of type 'IList'(x2)
            return false;
    }
    return true;
} …
Run Code Online (Sandbox Code Playgroud)

c# arrays

62
推荐指数
4
解决办法
11万
查看次数

比较两个List <string>是否相等

除了单独逐步执行元素之外,如何比较两个字符串列表是否相等(在.NET 3.0中):

这失败了:

// Expected result.
List<string> expected = new List<string>();
expected.Add( "a" );
expected.Add( "b" );
expected.Add( "c" );

// Actual result
actual = new List<string>();
actual.Add( "a" );
actual.Add( "b" );
actual.Add( "c" );

// Verdict
Assert.IsTrue( actual == expected );
Run Code Online (Sandbox Code Playgroud)

.net c# collections comparison equality

60
推荐指数
5
解决办法
4万
查看次数

标签 统计

c# ×2

.net ×1

arrays ×1

collections ×1

comparison ×1

equality ×1