C# 比较2个列表<&燃气轮机;不使用Linq
我正在处理2个列表,我想看看main是否包含相同的类型。这两个列表不需要包含相同的计数或顺序,只需包含所有匹配类型即可。我知道这是非常有可能与Linq,但我不能使用它C# 比较2个列表<&燃气轮机;不使用Linq,c#,list,C#,List,我正在处理2个列表,我想看看main是否包含相同的类型。这两个列表不需要包含相同的计数或顺序,只需包含所有匹配类型即可。我知道这是非常有可能与Linq,但我不能使用它 private static bool ContentsMatch(List<Type> list1, List<Type> list2) { if (list1.Count != list2.Count) return false;
private static bool ContentsMatch(List<Type> list1, List<Type> list2)
{
if (list1.Count != list2.Count)
return false;
for (int i = 0; i < list1.Count; i++)
{
if (!list1[i].Equals(list2[i]))
return false;
}
return true;
}
私有静态bool ContentsMatch(列表1、列表2)
{
if(list1.Count!=list2.Count)
返回false;
for(int i=0;i
我尝试的上述方法只有在它们的顺序相同时才会返回true。您可以使用C#关键字“is”来查看对象是否与给定类型兼容。
您可以使用C#关键字“is”查看对象是否与给定类型兼容。
假设您的意思是两个
列表
都有匹配的T
,您可以使用:
private static Boolean MatchingBaseType(IEnumerable a, IEnumerable b)
{
return GetIListBaseType(a) == GetIListBaseType(b);
}
private static Type GetIListBaseType(IEnumerable a)
{
foreach (Type interfaceType in a.GetType().GetInterfaces())
{
if (interfaceType.IsGenericType &&
(interfaceType.GetGenericTypeDefinition() == typeof(IList<>) ||
interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
)
{
return interfaceType.GetGenericArguments()[0];
}
}
return default(Type);
}
私有静态布尔匹配基类型(IEnumerable a,IEnumerable b)
{
返回GetIListBaseType(a)==GetIListBaseType(b);
}
私有静态类型GetIListBaseType(IEnumerable a)
{
foreach(在a.GetType().GetInterfaces()中键入interfaceType)
{
if(interfaceType.IsGenericType&&
(interfaceType.GetGenericTypeDefinition()==typeof(IList)||
interfaceType.GetGenericTypeDefinition()==typeof(IEnumerable)||
interfaceType.GetGenericTypeDefinition()==typeof(ICollection))
)
{
返回interfaceType.GetGenericArguments()[0];
}
}
返回默认值(类型);
}
您说计数不重要(尽管您正在检查
.count()
--为什么?),但如果两个列表中的类型相同,则应返回该值。假设您的意思是这两个列表
都具有匹配的t
,您可以使用:
private static Boolean MatchingBaseType(IEnumerable a, IEnumerable b)
{
return GetIListBaseType(a) == GetIListBaseType(b);
}
private static Type GetIListBaseType(IEnumerable a)
{
foreach (Type interfaceType in a.GetType().GetInterfaces())
{
if (interfaceType.IsGenericType &&
(interfaceType.GetGenericTypeDefinition() == typeof(IList<>) ||
interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
)
{
return interfaceType.GetGenericArguments()[0];
}
}
return default(Type);
}
私有静态布尔匹配基类型(IEnumerable a,IEnumerable b)
{
返回GetIListBaseType(a)==GetIListBaseType(b);
}
私有静态类型GetIListBaseType(IEnumerable a)
{
foreach(在a.GetType().GetInterfaces()中键入interfaceType)
{
if(interfaceType.IsGenericType&&
(interfaceType.GetGenericTypeDefinition()==typeof(IList)||
interfaceType.GetGenericTypeDefinition()==typeof(IEnumerable)||
interfaceType.GetGenericTypeDefinition()==typeof(ICollection))
)
{
返回interfaceType.GetGenericArguments()[0];
}
}
返回默认值(类型);
}
您说count不重要(尽管您正在检查
.count()
--为什么?),但如果两个列表中的类型相同,则应该返回该值。要比较任何用户定义的自定义类型,我们需要覆盖Equals&GetHashCode。
以下是您可以参考的代码段:
public class CustomizedDataType
{
private int field1;
private string field2;
public CustomizedDataType(int field1,string field2)
{
this.field1 = field1;
this.field2 = field2;
}
public override bool Equals(object obj)
{
CustomizedDataType dataType = obj as CustomizedDataType;
if (this.field1 == dataType.field1 && this.field2 == dataType.field2)
{
return true;
}
return false;
}
public override int GetHashCode()
{
return (this.field1.GetHashCode() + this.field2.GetHashCode());
}
要执行的示例代码:
static void Main(string[] args)
{
//Test Data
List<CustomizedDataType> dataTypeContaineer1 = new List<CustomizedDataType>();
dataTypeContaineer1.Add(new CustomizedDataType(10,"Test10"));
dataTypeContaineer1.Add(new CustomizedDataType(11, "Test11"));
dataTypeContaineer1.Add(new CustomizedDataType(12, "Test12"));
//Test Data
List<CustomizedDataType> dataTypeContaineer2 = new List<CustomizedDataType>();
dataTypeContaineer2.Add(new CustomizedDataType(100, "Test10"));
dataTypeContaineer2.Add(new CustomizedDataType(11, "Test11"));
dataTypeContaineer2.Add(new CustomizedDataType(12, "Test120"));
//Checking if both the list contains the same types.
if (dataTypeContaineer1.GetType() == dataTypeContaineer2.GetType())
{
//Checking if both the list contains the same count
if (dataTypeContaineer1.Count == dataTypeContaineer2.Count)
{
//Checking if both the list contains the same data.
for (int index = 0; index < dataTypeContaineer1.Count; index++)
{
if(!dataTypeContaineer1[index].Equals(dataTypeContaineer2[index]))
{
Console.WriteLine("Mismatch @ Index {0}", index);
}
}
}
}
}
static void Main(字符串[]args)
{
//测试数据
List dataTypeContaineer1=新列表();
添加(新的定制数据类型(10,“Test10”);
添加(新的定制数据类型(11,“Test11”);
添加(新的定制数据类型(12,“Test12”);
//测试数据
List dataTypeContaineer2=新列表();
添加(新的定制数据类型(100,“Test10”);
添加(新的定制数据类型(11,“Test11”);
添加(新的定制数据类型(12,“Test120”);
//检查两个列表是否包含相同的类型。
如果(DataTypeContainer1.GetType()==DataTypeContainer2.GetType())
{
//检查两个列表是否包含相同的计数
if(dataTypeContaineer1.Count==dataTypeContaineer2.Count)
{
//检查两个列表是否包含相同的数据。
for(int index=0;index
输出:
要比较任何用户定义的自定义类型,我们需要覆盖Equals&GetHashCode。 以下是您可以参考的代码段:
public class CustomizedDataType
{
private int field1;
private string field2;
public CustomizedDataType(int field1,string field2)
{
this.field1 = field1;
this.field2 = field2;
}
public override bool Equals(object obj)
{
CustomizedDataType dataType = obj as CustomizedDataType;
if (this.field1 == dataType.field1 && this.field2 == dataType.field2)
{
return true;
}
return false;
}
public override int GetHashCode()
{
return (this.field1.GetHashCode() + this.field2.GetHashCode());
}
要执行的示例代码:
static void Main(string[] args)
{
//Test Data
List<CustomizedDataType> dataTypeContaineer1 = new List<CustomizedDataType>();
dataTypeContaineer1.Add(new CustomizedDataType(10,"Test10"));
dataTypeContaineer1.Add(new CustomizedDataType(11, "Test11"));
dataTypeContaineer1.Add(new CustomizedDataType(12, "Test12"));
//Test Data
List<CustomizedDataType> dataTypeContaineer2 = new List<CustomizedDataType>();
dataTypeContaineer2.Add(new CustomizedDataType(100, "Test10"));
dataTypeContaineer2.Add(new CustomizedDataType(11, "Test11"));
dataTypeContaineer2.Add(new CustomizedDataType(12, "Test120"));
//Checking if both the list contains the same types.
if (dataTypeContaineer1.GetType() == dataTypeContaineer2.GetType())
{
//Checking if both the list contains the same count
if (dataTypeContaineer1.Count == dataTypeContaineer2.Count)
{
//Checking if both the list contains the same data.
for (int index = 0; index < dataTypeContaineer1.Count; index++)
{
if(!dataTypeContaineer1[index].Equals(dataTypeContaineer2[index]))
{
Console.WriteLine("Mismatch @ Index {0}", index);
}
}
}
}
}
static void Main(字符串[]args)
{
//测试数据
List dataTypeContaineer1=新列表();
添加(新的定制数据类型(10,“Test10”);
添加(新的定制数据类型(11,“Test11”);
添加(新的定制数据类型(12,“Test12”);
//测试数据
List dataTypeContaineer2=新列表();
添加(新的定制数据类型(100,“Test10”);
添加(新的定制数据类型(11,“Test11”);
添加(新的定制数据类型(12,“Test120”);
//检查两个列表是否包含相同的类型。
如果(DataTypeContainer1.GetType()==DataTypeContainer2.GetType())
{
//检查两个列表是否包含相同的计数
if(dataTypeContaineer1.Count==dataTypeContaineer2.Count)
{
//检查两个列表是否包含相同的数据。
for(int index=0;index
输出:
注释中提供的算法代码 不依赖于订单、计数或重复项目。也是一般的和抽象的
bool IsSameSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
return IsSubSet(l1, l2) && IsSubSet(l2, l1);
}
bool IsSubSet<T>(IEnumerable<T> l1, IEnumerable<T> l2)
{
var lookup = new Dictionary<T, bool>();
foreach (var e in l1)
lookup[e] = true;
foreach (var e in l2)
if (!lookup.ContainsKey(e))
return false;
return true;
}
针对用户的练习:
将附加参数添加到