我正在比较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,但它只是给出相等的结果。我不确定我是否需要在这里使用别的东西。
答案 0 :(得分:-1)
将代码缩减到我认为的相关位......
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
你在哪里看到错误的结果?
您的代码/比较正常。