💾 Paweł Łukasik<p>Sometimes there's a need to compile certain part of the code depending of the external factors like target OS or whether it's debug or release build. There is preprocessor directive <a href="https://infosec.exchange/tags/if" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>if</span></a> that allows to wrap a part of the code the be conditionally compiled but there are certain disadvantages to using that:</p><p>📌compiler doesn't check the code that is inside <a href="https://infosec.exchange/tags/if" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>if</span></a> directive when the condition is not true. That could lead to discovering an error once the directive changes to true due to other part of the system had changed.</p><p>📌with <a href="https://infosec.exchange/tags/if" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>if</span></a> it's often that the directive would be inside the method at the call site, mixed up with the generic code which makes that harder to read</p><p>Conditional attribute can fix those problems. We only need to mark the method that supposed to be conditionally compiled into the code. We do not need to change the call-site and the compiler takes care of that. Also, the compiler still checks the correctness of the code that we have inside that method.</p><p>🚩Docs for preprocessor directives📑 <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">learn.microsoft.com/en-us/dotn</span><span class="invisible">et/csharp/language-reference/preprocessor-directives#conditional-compilation</span></a></p><p>🚩Docs for Conditional attribute 📑 <a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=net-8.0" rel="nofollow noopener" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">learn.microsoft.com/en-us/dotn</span><span class="invisible">et/api/system.diagnostics.conditionalattribute?view=net-8.0</span></a></p><p>Did you had to use those in your projects? If so please share ♻.</p><p><a href="https://infosec.exchange/tags/dotnet" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>dotnet</span></a> <a href="https://infosec.exchange/tags/preprocessor" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>preprocessor</span></a> <a href="https://infosec.exchange/tags/conditionalattribute" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>conditionalattribute</span></a></p>