As I commented before you can use myList.All(LessThan16Char)
.
Keep in mind that myList.All(LessThan16Char)
is different from myList.All(x => LessThan16Char(x))
.
The second one creates an extra indirection. Compiler translates x => LessThan16Char(x)
to a method which gets an string as an input and call LessThan16Char
for it.
You can see the different IL generated.
1 . myList.All(LessThan16Char)
IL_0008: ldarg.0
IL_0009: ldftn UserQuery.LessThan16Char
IL_000F: newobj System.Func<System.String,System.Boolean>..ctor
IL_0014: call System.Linq.Enumerable.All
2 . myList.All(x=> LessThan16Char(x))
IL_001B: ldarg.0
IL_001C: ldftn UserQuery.<Main>b__0_0
IL_0022: newobj System.Func<System.String,System.Boolean>..ctor
IL_0027: call System.Linq.Enumerable.All
and extra generated method
<Main>b__0_0:
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call UserQuery.LessThan16Char
IL_0007: ret
Usually they both do the same thing but in some scenarios they can be different. For example when you want to know the class which contains the method passed to the LINQ query and you are passing int.Parse
instead of x => int.Parse(x)
. The second one is a method inside your class but first one is on a framework classes.