Click here to Skip to main content
15,908,661 members
Home / Discussions / C#
   

C#

 
AnswerRe: List Comparison Pin
DaveyM6910-Jun-10 5:10
professionalDaveyM6910-Jun-10 5:10 
An ideal candidate for generics
C#
bool CompareLists<T>(List<T> listA, List<T> listB)
{
    if (listA.Count != listB.Count) 
        return false;
    foreach (T item in listA)
        if (!listB.Contains(item))
            return false;
    return true;
}


Edit: I haven't optimised your code at all - just converted it to accept generics. You should probably null check both parameters - if both are null it's up to you how you want it to return. As this is nothing really to do with an actual class it may be useful to make it static and possibly an extension method if using 3.0 or above.
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: List Comparison Pin
Paladin200010-Jun-10 5:28
Paladin200010-Jun-10 5:28 
GeneralRe: List Comparison Pin
Hristo-Bojilov10-Jun-10 5:52
Hristo-Bojilov10-Jun-10 5:52 
AnswerRe: List Comparison Pin
Luc Pattyn10-Jun-10 5:30
sitebuilderLuc Pattyn10-Jun-10 5:30 
GeneralRe: List Comparison Pin
Paladin200010-Jun-10 5:39
Paladin200010-Jun-10 5:39 
GeneralRe: List Comparison Pin
#realJSOP10-Jun-10 6:36
professional#realJSOP10-Jun-10 6:36 
AnswerRe: List Comparison Pin
Paladin200010-Jun-10 5:58
Paladin200010-Jun-10 5:58 
GeneralRe: List Comparison Pin
Paladin200010-Jun-10 6:06
Paladin200010-Jun-10 6:06 
GeneralRe: List Comparison Pin
Luc Pattyn10-Jun-10 6:06
sitebuilderLuc Pattyn10-Jun-10 6:06 
AnswerRe: List Comparison Pin
Kevin McFarlane10-Jun-10 10:45
Kevin McFarlane10-Jun-10 10:45 
QuestionGoogle Maps Pin
JohnUSA10-Jun-10 2:53
JohnUSA10-Jun-10 2:53 
AnswerMessage Closed Pin
10-Jun-10 3:06
stancrm10-Jun-10 3:06 
GeneralRe: Google Maps Pin
JohnUSA10-Jun-10 4:45
JohnUSA10-Jun-10 4:45 
AnswerRe: Google Maps Pin
Paladin200010-Jun-10 4:41
Paladin200010-Jun-10 4:41 
GeneralRe: Google Maps Pin
JohnUSA10-Jun-10 4:53
JohnUSA10-Jun-10 4:53 
GeneralRe: Google Maps Pin
Paladin200010-Jun-10 5:20
Paladin200010-Jun-10 5:20 
AnswerRe: Google Maps Pin
Adam R Harris10-Jun-10 4:52
Adam R Harris10-Jun-10 4:52 
QuestionTricky Inheritance Question Pin
Jon Myers10-Jun-10 2:47
Jon Myers10-Jun-10 2:47 
AnswerRe: Tricky Inheritance Question Pin
Andrew Rissing10-Jun-10 4:21
Andrew Rissing10-Jun-10 4:21 
GeneralRe: Tricky Inheritance Question Pin
Jon Myers10-Jun-10 5:40
Jon Myers10-Jun-10 5:40 
GeneralRe: Tricky Inheritance Question Pin
Andrew Rissing10-Jun-10 7:34
Andrew Rissing10-Jun-10 7:34 
QuestionAuto serial software Pin
ostad_mrn10-Jun-10 2:39
ostad_mrn10-Jun-10 2:39 
AnswerRe: Auto serial software Pin
#realJSOP10-Jun-10 2:49
professional#realJSOP10-Jun-10 2:49 
QuestionRe: Auto serial software Pin
ostad_mrn10-Jun-10 3:13
ostad_mrn10-Jun-10 3:13 
AnswerRe: Auto serial software Pin
#realJSOP10-Jun-10 4:26
professional#realJSOP10-Jun-10 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.