Code inside C# Preprocessor Directives treated like dead code

I noticed that code inside C# Preprocessor Directives (example: #if DEBUG)  are treated like dead code. They are greyed and I can't use R# navigation shortcuts on them. Is this by design? I would preferred the code can be accessed like active real code.

0
2 comments
Avatar
Permanently deleted user

as far as i know R# evaluates  the expression. So if you have a "#if DEBUG" and you didn't defined a "DEBUG" constant in your build configuration, R# doesn't inspect the code inside the directive. Normally the DEBUG constant is set by default, if you use a debug build configuration. I checked the behavior with R# 9.1.1 and it works as expected for me.

Regards
Klaus

0

Yes, this is by design, because the C# compiler doesn't see the code inside the preprocessor directives, either. This means that code inside these directives doesn't actually have to be code, and everything is fine - it can be duplicate code, or broken code, or not even C# syntax, and there are no warnings or errors. So, looking at this from the other direction, if ReSharper processed what was inside the directive, it could encounter broken or duplicate code - how should it handle this? And ReSharper doesn't know *why* you've wrapped the code in a preprocessor directive. You might have temporarily wrapped some code in a #if so that you can write a new version of the code, experimenting with a new algorithm. If ReSharper parsed the contents, should it rename variables when you do a refactoring, or include the commented out code in control flow analysis? But that could break your code when you remove the #if. Basically, the contents of preprocessor directives are undefined, and understanding how to reliably do something useful with that content in a manner that integrates with existing functionality sensibly is a really hard problem to solve. There are too many "what if" issues.

On a more positive note, ReSharper does handle #if directives when a file is included in mutliple contexts - shared projects or linked files. The file is parsed in the context of each project, so might have different preprocessor values defined. ReSharper will then understand what's inside the #if blocks, and treat them as code, and include them in refactorings - because they are code, and will compile or fail when the correct values are defined.

0

Please sign in to leave a comment.