Inspired by the discussion about "unnecessary ToArray() method call", i decide to post this answer to bring more information about the "differences" between methods used in a both solutions (solution 1 and solution 2).
The most important statement is:
Both solutions are equivalent and return exactly the same result!
Even if there are few differences (in a scope of code compilation and execution - see below table), it doesn't matter in this case! It might makes a difference only in case when the set of strings comparison is performed.
Solution 1 | Solution 2 |
query syntax | method syntax |
IL instructions (generated by LinqPad 4):
IL_0001: ldstr "Jack#Alex#Marlo#Jimmy#Bob"
IL_0006: stloc.0 // s
IL_0007: ldloc.0 // s
IL_0008: call System.Linq.Enumerable.ToArray
IL_000D: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0012: brtrue.s IL_0027
IL_0014: ldnull
IL_0015: ldftn b__0
IL_001B: newobj System.Func<System.Char,System.Boolean>..ctor
IL_0020: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0025: br.s IL_0027
IL_0027: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_002C: call System.Linq.Enumerable.Where
IL_0031: call System.Linq.Enumerable.Count
IL_0036: stloc.1 // SharpCount
b__0:
IL_0000: ldarg.0
IL_0001: ldc.i4.s 23
IL_0003: ceq
IL_0005: stloc.0 // CS$1$0000
IL_0006: br.s IL_0008
IL_0008: ldloc.0 // CS$1$0000
IL_0009: ret
| IL instructions (generated by LinqPad 4):
IL_0001: ldstr "Jack#Alex#Marlo#Jimmy#Bob"
IL_0006: stloc.0 // theString
IL_0007: ldloc.0 // theString
IL_0008: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_000D: brtrue.s IL_0022
IL_000F: ldnull
IL_0010: ldftn b__0
IL_0016: newobj System.Func<System.Char,System.Boolean>..ctor
IL_001B: stsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0020: br.s IL_0022
IL_0022: ldsfld UserQuery.CS$<>9__CachedAnonymousMethodDelegate1
IL_0027: call System.Linq.Enumerable.Count
IL_002C: stloc.1 // cntNumberSign
b__0:
IL_0000: ldarg.0
IL_0001: ldc.i4.s 23
IL_0003: ceq
IL_0005: stloc.0 // CS$1$0000
IL_0006: br.s IL_0008
IL_0008: ldloc.0 // CS$1$0000
IL_0009: ret
|
Not uses lambda expressions, but compiler changes the query expression into the equivalent Lambda expression
| Uses lambda expressions, so compiler doesn't need to "change" anything.
|
Time of code execution: 00:00.000
| Time of code execution: 00:00.000
|
Query used in this solution is equivalent to:
int SharpCount = s.Where(c=>c=='#').Select(c=>c).Count();
| Query used in this solution does not use Select method.
int cntNumberSign = theString.Count(c => c == '#');
|
Conclusion:
There is only one (the most important) difference: first solution uses "unnecessary"
Select
method, but not
ToArray()
! ;) In both cases
ToArray()
method is used. In the second one,
ToArray
method is used indirect, because delegate method must split characters to array to be able to compare each character to '
#
'.
For further information, please see:
Query Syntax and Method Syntax in LINQ (C#)[
^]
Lambda Expressions (C# Programming Guide)[
^]
Thank you, anonymous downvoter! You've been my inspiration to take a bit deeper analysis.