C # Linq Char Except () arrays - strange behavior

I find it difficult to understand why this test ended with the message "Assert.AreEqual failed. Expected: <2>. Actual: <1>."

[TestMethod] public void Test() { char[] a1 = "abc".ToCharArray(); char[] a2 = {'a', 'b', 'c', ' ', ' '}; Assert.AreEqual(2, a2.Except(a1).Count()); } 

but the following:

  [TestMethod] public void Test() { char[] a1 = "abc".ToCharArray(); char[] a2 = {'a', 'b', 'c', ' ', 'd', ' '}; Assert.AreEqual(2, a2.Except(a1).Count()); } 
+6
source share
4 answers

since it excludes detection of the difference of two sequences

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

maybe you need something like this

 var c=a2.Where(a=>a1.Contains(a)==false).Count(); 
+2
source

In addition, you get SET , which means that it does not return duplicates.

See Excluding Documentation

+4
source

In addition, you get a great list.

 char[] a1 = "abc".ToCharArray(); char[] a2 = {'a', 'b', 'c', ' ', '1'}; Assert.AreEqual(2, a2.Except(a1).Count()); // Passes 
+3
source

The Except function returns the set difference of two sequences - not a difference .

The space character is returned only once.

+3
source

Source: https://habr.com/ru/post/914905/


All Articles