G
Guest
This is probably common knowledge to .Net gurus, but please bear with me
while I share a discovery with the group...
I needed to create a public lock on a class, as follows...
class Locked
{
.....
public object lockObject = new object();
}
Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on it,...
class Locked
{
.....
private object lockObject = new object();
public object LockObject
{
get {return lockObject;}
}
}
However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really is
an overhead, so ran ildasm on both versions of the exe. The dissasembled code
for the client was identical in both cases!
ie.
Locked l = new l;
lock (l.lockObject) {..}; // public member, no accessor
lock (l.LockObject()) {...}; // private member, "get" accessor
both compile to the same code. In other words, the compiler removes the
method call associated with the "get", and does it "in-line" (in C++
parlance).
The lesson... don't avoid accessors because of any perceived performance
issues.
Regards,
Javaman
while I share a discovery with the group...
I needed to create a public lock on a class, as follows...
class Locked
{
.....
public object lockObject = new object();
}
Next I wanted to consider hiding the lockObject, according to good OO
practice, by making it private, and putting a public "get" accessor on it,...
class Locked
{
.....
private object lockObject = new object();
public object LockObject
{
get {return lockObject;}
}
}
However, I hesitated, because I didn't want the "overhead" of the accesor
each time a client gets a lock. I was curious as to whether there really is
an overhead, so ran ildasm on both versions of the exe. The dissasembled code
for the client was identical in both cases!
ie.
Locked l = new l;
lock (l.lockObject) {..}; // public member, no accessor
lock (l.LockObject()) {...}; // private member, "get" accessor
both compile to the same code. In other words, the compiler removes the
method call associated with the "get", and does it "in-line" (in C++
parlance).
The lesson... don't avoid accessors because of any perceived performance
issues.
Regards,
Javaman