In C# (or VB .NET), does the compiler make attempts to optimize property accesses? For eg.,
public ViewClass View
{
get
{
...
Something is computed here
....
}
}
if (View != null)
View.Something = SomethingElse;
I would imagine that if the compiler could somehow detect that View
remains constant between the two accesses, it can refrain from computing the value twice. Are these kind of optimizations performed?
I understand that if View
has some intensive computations, it should probably be refactored into a function (GetView()
). In my particular case, View
involves climbing the visual tree looking for an element of a particular type.
Related: Any references on the workings of the (Microsoft) C# compiler?