Home > DeveloperSection > Forums > Simplest an fastest C# Expression that always evaluates to false and does not generate a compiler warning
Goti Bandu

Total Post:119

Points:835
Posted on    May-13-2013 3:14 AM

 C# C# 
Ratings:


 1 Reply(s)
 896  View(s)
Rate this:
Hi Everyone!

I have a rather esoteric case where I need to have some piece of code that will never execute but which still needs to be part of the compilation output. Although I could come up with my own, I'm asking the community: What is the simplest and fastest C# Expression that always evaluates to false and does not generate a compiler warning and preserves the enclosed block of code in the final build output?

Update: Since explaining the rationale behind the question was requested: The Mono touch linker performs static code analysis in order to strip all unreferenced symbols out of the final build to decrease the size of the generated binaries. This poses a problem with properties only accessed through reflection.

Any help will be appreciated! 
Thanks in advance!





AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-13-2013 9:33 AM

Hi Goti!

Try as following

public static bool False() {
    return false;
}
public static void Foo() {
    if (False()) {
       ...
    }
}

If you need to convince JIT too (inlining), then add [MethodImpl(MethodImplOptions.NoInlining)] to False()

Don't want to miss updates? Please click the below button!

Follow MindStick