How can the cyclomatic complexity be 27 in a method with 13 event handler subscriptions?

Remember that the Code Analysis is looking at the IL in your assembly, not your source code. There is nothing in the IL that natively supports lambda expressions, so they are a construct of the compiler. You can find the specifics of what is output here. But basically your lambda expression is turned into a private static class that is an anonymous delegate. However, rather that create an instance of the anonymous delegate every time it is referenced in code, the delegate is cached. So each time you assign a lambda expression, it does a check to see an instance of that lambda delegate has been created, if so it uses the cached delegate. That generates an if/else in the IL increasing the complexity by 2. So in this functions complexity is 1 + 2*(lambda express) = 1 + 2 *(13) = 27 which is the correct number.


The C# compiler actually generates some fairly "interesting" IL for anonymous methods, including lambdas. For each one, it creates a private field then, before assigning its value in the consuming method, it checks if the value is null, which adds an If branch to the compiled method. The code metrics tool ought to ignore this (http://social.msdn.microsoft.com/Forums/eu/vstscode/thread/8c17f569-5ee3-4d26-bf09-4ad4f9289705, https://connect.microsoft.com/VisualStudio/feedback/details/555560/method-using-many-lambda-expressions-causes-high-cyclomatic-complexity), and we can hope that it eventually will. For now, you pretty much have to ignore the problem if you feel that it is a false positive.