Remove unreachable code refactoring
This refactoring applies to:
C#
Visual Basic
What: Removes code that will never be executed.
When: Your program has no path to a code snippet, making that code snippet unnecessary.
Why: Improve readability and maintainability by removing code that is superfluous and will never be executed.
How-to
- Place your cursor anywhere in the faded out code that is unreachable:
Next, do one of the following:
- Keyboard
- Press Ctrl+. to trigger the Quick Actions and Refactorings menu and select Remove unreachable code from the Preview window popup.
- Mouse
- Right-click the code, select the Quick Actions and Refactorings menu and select Remove unreachable code from the Preview window popup.
- Keyboard
When you're happy with the change, press Enter or click the fix in the menu and the changes will be committed.
Example:
// Before
private void Method()
{
throw new Exception(nameof(Method));
Console.WriteLine($"Exception for method {nameof(Method)}");
}
// Remove unreachable code
// After
private void Method()
{
throw new Exception(nameof(Method));
}
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for