Will non-virtual properties to constant values be evaluated in compile time? (Like the constants themselves)
Example:
class Clazz
{
const int SPEED = 5;
public int Speed
{
get { return SPEED; }
}
}
I know that any call to the constant SPEED will be evaluated once at compile time, but if I access the Clazz.Speed property from anywhere in my program, will that also be evaluated at compile time?
Edit: To evlove the question a little bit, will the next two examples be inlined by the Compiler (and not the JIT)? [ie. evaluated during compile time] :
// a static / non-static method that returns a constant value
(static) int GetConstant() { return 42; }
// a static / non-static property that returns a constant value
(static) int ConstProperty { get { return 42; } }
Running this through Linqpad produces the following IL for the getter:
This would mean that the getter is called at runtime (and not inlined) and returns a constant value.
Hypothetically if the getter would be inlined in this case that would mean that the implementation of the property could not be changed without having to recompile any consuming assemblies – this would defeat using a property in the first place.