簡體   English   中英

C#列表未與equals運算符進行比較

[英]C# List not comparing with equals operator

我正在用C#比較兩個列表。 這是基本代碼

List<string> expectedTabs = new List<string>() { "Batch", "Card Services", "ReportingXXXXX" };

List<string> actualTabs = new List<string>() { "Batch", "Card Services", "Reporting" };

string [] text = new string[actualTabs.Count];

int i = 0;

foreach (IWebElement element in actualTabs)
{
     text[i++] = element;
     Console.WriteLine("Tab Text : " + text[i - 1]);
}

for(int x = 0; x < actualTabs.Count; x++)
{
    Assert.IsTrue(expectedTabs.Count == actualTabs.Count); //this gives equal count
    Console.WriteLine(expectedTabs[x] + " :: " + actualTabs[x]);

    expectedTabs[x].Equals(actualTabs[x]); //Gives wrong result here

    Console.WriteLine(expectedTabs.GetType()); //returns type as collection.list
}

現在,當比較兩個列表中的最后一個元素[ReportingXXXXX and Reporting]時,equal應該返回false,但是它只給出相等的結果。 我不確定是否需要在這里使用其他內容。

將您的代碼減少到我認為是相關的位...

var expectedTabs = new List<string>() { "Batch", "Card Services", "ReportingXXXXX" };
var actualTabs = new List<string>() { "Batch", "Card Services", "Reporting" };
for (var x = 0; x < actualTabs.Count; x++)
{
   var equal = expectedTabs[x].Equals(actualTabs[x]); //Gives wrong result here
   Console.WriteLine(equal);
}

這產生

True
True
False

您在哪里看到錯誤的結果?

您的代碼/比較工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM