检查两个StringCollection对象在C#中是否相等
要检查两个StringCollection对象是否相等,代码如下-
示例
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol1 = new StringCollection();
strCol1.Add("Accessories");
strCol1.Add("Books");
strCol1.Add("Electronics");
Console.WriteLine("StringCollection1 elements...");
foreach (string res in strCol1) {
Console.WriteLine(res);
}
StringCollection strCol2 = new StringCollection();
strCol2.Add("Accessories");
strCol2.Add("Books");
strCol2.Add("Electronics");
Console.WriteLine("StringCollection2 elements...");
foreach (string res in strCol1) {
Console.WriteLine(res);
}
Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2));
}
}输出结果
这将产生以下输出-
StringCollection1 elements... Accessories Books Electronics StringCollection2 elements... Accessories Books Electronics Both the String Collections are equal? = False
示例
让我们看另一个例子-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol1 = new StringCollection();
strCol1.Add("Accessories");
strCol1.Add("Books");
strCol1.Add("Electronics");
Console.WriteLine("StringCollection1 elements...");
foreach (string res in strCol1) {
Console.WriteLine(res);
}
StringCollection strCol2 = new StringCollection();
strCol2.Add("Accessories");
strCol2.Add("Books");
strCol2.Add("Electronics");
Console.WriteLine("StringCollection2 elements...");
foreach (string res in strCol1) {
Console.WriteLine(res);
}
Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2));
StringCollection strCol3 = new StringCollection();
strCol3 = strCol2;
Console.WriteLine("Is StringCollection3 equal to StringCollection2? = "+strCol3.Equals(strCol2));
}
}输出结果
这将产生以下输出-
StringCollection1 elements... Accessories Books Electronics StringCollection2 elements... Accessories Books Electronics Both the String Collections are equal? = False Is StringCollection3 equal to StringCollection2? = True
热门推荐
10 生日祝福语简短蛋糕上
11 老公生日祝福语简短好看
12 生日祝福语简短激励女生
13 恩师退休花束祝福语简短
14 七一祝福语明信片文案简短
15 祝福语简短七夕
16 小孩想说的祝福语简短
17 分手快乐祝福语简短
18 老师节祝福语的简短