This will not be optimized - the getter is a method and will be called, although the JIT may choose to "embed" it a lot, if it is simple, perhaps eventually (and completely theoretically), ending just with a null-reference check if TheProp does nothing special interesting. What can happen is that the “thing returned” (the code is stored in x ) has the right to garbage collection until the end of the method; this is rarely a problem, with the exception of things like mutexes outside the process; in this case you can do things like:
var x = obj.TheProp;
Please note that if x entered, it will force it to be boxed, which is not ideal.
However: the above is not intended to ensure that TheProp actually called; it is intended for a specific set of garbage-related scenarios.
source share