Compare Two List of Strings in C# and VB.NET

Imagine you have two List<String>. You want to quickly compare them to see if all the elements match each other. Here's how to do so:

C#


List<string> strList1 = new List<string>


{


    "Jack", "And", "Jill", "Went", "Up", "The", "Hill"


};


 


List<string> strList2 = new List<string>


{


    "Jack", "And", "Jill", "Went", "Down", "The", "Hill"


};


 


bool result = strList1.SequenceEqual(strList2);




VB.NET


        Dim strList1 As List(Of String) = New List(Of String)(New String() _


        {"Jack", "And", "Jill", "Went", "Up", "The", "Hill"})


 


        Dim strList2 As List(Of String) = New List(Of String)(New String() _


         {"Jack", "And", "Jill", "Went", "Down", "The", "Hill"})


 


        Dim result As Boolean = strList1.SequenceEqual(strList2)




Remember that this search is case sensitive!

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

4 comments:

Anonymous said...

Thank You.

Anonymous said...

muchas gracias estaba buscando esto, me ha sido de mucha ayuda

Saravanan said...

I was spending more than 4 hours for this and finally solved with a simple approach. THank you so much.

INBAKALAYHEESWARAN CHINNAKALTHY said...

hi,
i am new in this blog.i have struggeled for Comparision of two list. the list data contains like that.

List 1
22 KV,11 KV,110 KV

list 2
22 11 115

Actual My requirement was if List 1 having 22 KV and then List 2 value is 22 or 24.
please give me solution or idea.


Thanks in Advance.