Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the object?
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Sat, 22 Apr 2006 11:16:19 +0200
"Michael C" <nospam@xxxxxxxxxx> wrote in message
news:%23NTWbvdZGHA.1196@xxxxxxxxxxxxxxxxxxxxxxx
| "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
| news:MPG.1eb3db3bf24bd01398d12a@xxxxxxxxxxxxxxxxxxxxxxx
| > For me, a good reason is: "there may not be any validation now, but I
| > suspect there might be in the future".
|
| It all depends on the situation but I think it is rare that the class
itself
| needs to access the variable though the property. 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. If it does change and it
becomes
| appropriate to use the property then modify the code.
|
| Michael
|
|
But that's the whole point, there is no slower access when you access the
property, the JIT will inline the property getter, so the result is a simple
direct field access (memory access)
Consider following sample:
class Test
{
int i;
int v;
int I {
get { return i;}
}
static void Main()
{
Random rnd = new Random();
Test t = new Test();
t.i = rnd.Next();
t.v = rnd.Next();
int iii = t.I;
int ii = t.v;
int sum = ii+iii;
Console.WriteLine(sum);
}
The JIT produces the following when run in the debugger...
' move OBJREF t into eax
00cb0115 8b45f0 mov eax,[ebp-0x10]
ss:0023:0012f470=01275ae4
' move field i value into eax !!!!
00cb0118 8b4004 mov eax,[eax+0x4]
move eax into edx
00cb011b 8bd0 mov edx,eax
' move OBJREF into eax
00cb011d 8b45f0 mov eax,[ebp-0x10]
' move field v into eax
00cb0120 8b4008 mov eax,[eax+0x8]
' add i to v store result in eax
00cb0123 03c2 add eax,edx
' move result into esi
00cb0125 8bf0 mov esi,eax
' use value in esi as argument for Console.WriteLine..
you see, the property getter and setter (in this simple case, is nothing
else than syntactic sugar, there is no such thing at the machine code level
as a (slower) function call.
Willy.
.
- 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: Exception Handling Question
- 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
- Index(es):
Relevant Pages
|