Opening the assembly in the reflector, the compiler optimizes the multiplication, since they const int:
public class FooContainer
{
private const int A = 2;
private const int B = 3;
public int Result
{
get
{
return A * B;
}
}
}
It will become:
public class FooContainer
{
private const int A = 2;
private const int B = 3;
public int Result
{
get
{
return 6;
}
}
}
But if you change the variables to simple intand don’t store the calculations, the evaluation will happen every time.
Example:
public class FooContainer
{
private int A = 2;
private const int B = 3;
public int Result
{
get
{
return A * B;
}
}
}
becomes:
public class FooContainer
{
private int A = 2;
private const int B = 3;
public int Result
{
get
{
return (this.A * 3);
}
}
}
source
share