c# - How to compare two List content data and return mismatch value? -


this question has answer here:

i have 2 list. content simple string values. example:

ilist1 content:

  • the avengers
  • shutter island
  • inception
  • the dark knight rises

list2 content:

  • the avengers
  • shutter island
  • inception
  • the dark knight rises
  • parks , recreation
  • scandal

i want compare 2 list , return mismatch value. in case return "parks , recreation" , "scandal" not matching values of list1.

i tried it. throws exception "object reference not set instance of object".

static void main(string[] args)     {         list<string> list1 = new list<string>();         list1.add("the avengers");         list1.add("shutter island");         list1.add("inception");         list1.add("the dark knight rises");          list<string> list2 = new list<string>();         list2.add("the avengers");         list2.add("shutter island");         list2.add("inception");         list2.add("the dark knight rises");         list2.add("parks , recreation");         list2.add("scandal");             try         {             list<string> difference = comparator(list1, list2);             foreach (var value in difference)             {                 console.writeline(value);             }         }         catch (system.nullreferenceexception e)         {             console.writeline(e.message);             }          console.readline();      }     public static list<string> comparator(list<string> list1, list<string> list2)     {         ienumerable<string> differencequery = list1.except(list2);         list<string> differ = null;          foreach (string s in differencequery)             differ.add(s);          return differ;     } 

can me out? in advance.

the problem line:

list<string> differ = null; 

after try add list, list null. there's exception.

you change follows:

list<string> differ = new list<string>(); 

but easier replace line:

list<string> difference = comparator(list1, list2); 

by:

list<string> difference = list1.except(list2).tolist(); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -