तुम इतनी तरह एक विस्तार विधि लिख सकते हैं:
public static class ListExtensions
{
public static bool IsEqual<T>(this IList<T> list,IList<T> target, IComparer<T> comparer) where T:IComparable<T>
{
if (list.Count != target.Count)
{
return false;
}
int index = 0;
while (index < list.Count &&
comparer.Compare(list[index],target[index]) == 0)
{
index++;
}
if (index != list.Count)
{
return false;
}
return true;
}
}
और यह इतनी तरह फोन:
List<int> intList = new List<int> { 1, 234, 2, 324, 324, 2 };
List<int> targetList = new List<int> { 1, 234, 2, 324, 324 };
bool isEqual = intList.IsEqual(targetList, Comparer<int>.Default);
संपादित करें: एक स्थिर विधि के बजाय के बाद से ओपी नेट उपयोग कर रहा है उपयोग करने के लिए कोड अपडेट किया गया 3,0
public static bool IsEqual<T>(IList<T> sourceList, IList<T> targetList, IComparer<T> comparer) where T : IComparable<T>
{
if (sourceList.Count != targetList.Count)
{
return false;
}
int index = 0;
while (index < sourceList.Count &&
comparer.Compare(sourceList[index], targetList[index]) == 0)
{
index++;
}
if (index != sourceList.Count)
{
return false;
}
return true;
}
ग्राहक:
bool isEqual = IsEqual(intList,targetList, Comparer<int>.Default);
स्रोत
2009-10-10 04:12:27
संभव डुप्लिकेट [क्या दो सूची सी # में समानता के लिए सूचियों की जांच करने के लिए सबसे अच्छा तरीका है] (http://stackoverflow.com/questions/876508/समान-सर्वोत्तम-तरीके-से-चेक-दो-सूची-सूचियों के लिए-समानता-इन-सी-तेज) –
nawfal