Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 22 Apr 2006 00:56:07 -0700
There is no point turning a high speed variable access into a slower function call every time just in case something might change in the future.
Only in debug mode. One of Bruce's Laws is "never try to
micro-optimize... the compiler is much smarter than you are!" :-)
In this case, somewhere in the compiler / JITter sequence the call to
the property getter is reduced to a simple variable access, so there is
no difference in performance whatsoever for release-compiled code.
That said, I'm not sure I agree with Jon. The property setter may
include functionality that you explicitly *don't* wan't to invoke from
within the class, like raising ...Changed events and things like that.
It all depends upon what you're doing and why, and how you want to
timing of things to work out. I have plenty of classes that want to
manipulate their state in some way and _only then_ determine which
....Changed events to raise as a result. I have other classes that
always set their own fields through the property setters specifically
in order to invoke additional behaviour such as ...Changed events.
It all depends upon what the setter does and what your class is trying
to do.
.
- Follow-Ups:
- References:
- C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: Dave
- Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: Michael C
- Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: Jon Skeet [C# MVP]
- Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: Michael C
- C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- Prev by Date: Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- Next by Date: Re: C# coding guidelines: use "this." or not when referring to member
- Previous by thread: Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- Next by thread: Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- Index(es):