I don’t have too much knowledge of compilers and how .Net optimizes the generated machine code but would like to understand the following scenario:
class AnyClass
{
public bool anyFlag;
AnyClass()
{
anyFlag = true;
}
public void Action()
{
if(anyFlag)
//Perform Certain Actions
}
}
anyFlag does not change throughout the scope of the program. Will the compiler generate MIL/machine code for Action method? If so will it have the if check in there?
In your case the compiler won’t filter the if statement because your anyFlag is public (so it can be changed from multiple places) and because the value is only is set in the consctructor the compiler also doesn’t now this value until runtime.
The following code does what you want:
If you insert this code in Visual Studio you will see a generated warning stating that s=”b”; will never be reached and this code will be optimized out.