Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest an fastest C# Expression that always evaluates to false and does not generate a compiler warning

Tags:

c#

xamarin.ios

I have a rather esotheric 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 Monotouch 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.

like image 797
Oliver Weichhold Avatar asked May 13 '13 07:05

Oliver Weichhold


1 Answers

I'd go for simple...

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()

like image 51
Marc Gravell Avatar answered Sep 24 '22 06:09

Marc Gravell