Chalk me up to the other camp. I think it's better to explicitly prefix
access to member variables with this. to differentiate them from locally
declared equivalents of the same name. I see alot of this going on in,
especially in constructors:
public class Foo
{
private int param1, param2, param3;
public Foo( int param1 )
{
this.param1 = param1;
}
public Foo( int param1, param2 )
{
this.param1 = param1;
this.param2 = param2;
}
...
public int DoSomeWork( int param1, int param2 )
{
int param3;
...
param3 = this.param1 / param1 + param2-this.param3;
}
}
Anyway, I picked up the practice from an ancient Microsoft coding guidelines
text, which I think they have since backed away from somewhat. I like it,
but its personal preference I guess.
On Tue, 18 Mar 2008 13:01:18 -0700, K Viltersten <tmp1@viltersten.com>
wrote:
> [...]
> However, one thing caught my attention. Are you for
[quoted text clipped - 8 lines]
> I'd go with "this" as this makes stuff very clear and
> explicitly stated, if yet somewhat redundant. What would you say?
I generally do not explicitly write "this". After all, in C# there are no
global identifiers, and so without a qualifying reference, any identifier
must be relative to "this".
Sometimes you need "this". For example, a very simple class where instead
of properties you've made readonly public fields, and you want to use the
same name for constructor parameters as for the field names, you need
"this" to assign the parameters to the fields (I use that example
specifically because I think it's one of the few, if not the only,
situations in which it's not a bad idea for parameters to hide class
members).
But otherwise, I don't bother. Which is not to say that I object to code
that does explicitly use "this". If someone else wants to write it that
way, that's fine with me. I just think that in C# it's obvious enough
without it.
Pete

Signature
Posted via a free Usenet account from http://www.teranews.com
Peter Duniho - 19 Mar 2008 00:45 GMT
> Chalk me up to the other camp. I think it's better to explicitly prefix
> access to member variables with this. to differentiate them from locally
> declared equivalents of the same name. I see alot of this going on in,
> especially in constructors:
Well, the example you provide is exactly what I was talking about wrt to
readonly public members. It only comes up for me in that situation
because the naming conventions I use result in field names that would
never be mistaken for a method parameter for private fields.
And I don't understand why you describe it as "better", when in fact it's
mandatory in that situation.
But yes, as I noted, where you have a situation where a parameter has the
same name as a field (or other member, though hopefully that would be even
more rare) you must use "this" to qualify the member.
That said, it's my opinion that even in situations where the same naming
convention might apply to a member field as to a locally defined variable,
that should _only_ happen in the case of a parameter, and then only in the
case of a constructor. If you're finding conflicts elsewhere, IMHO that's
a sign of a poor naming convention.
Pete