Search code examples
c#detectiondead-code

Detect dead code in C#


How can I detect dead code in my C# application?


Solution

  • Compile your code and check the warnings in the Error List. The following code:

        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
            return null;  // unreachable
        }
    

    produces this warning:

    Warning 11  Unreachable code detected   <fullpath>\HomeController.cs    13  13  <prjname>
    

    Tools like JetBrains ReSharper (http://jetbrains.com/resharper)* can also perform this analysis on the fly and highlight dead code.

    * ReSharper is a commercial tool.