Is this. needed?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(long n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Negative parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?

I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

Thanks,
Brett
 
Brett Romero said:
I'd like to know when exactly the this. keyword is needed. For
example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(long n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Negative parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?


No. The this in this case simply tells the compiler you mean the field, not
the local parameter.
I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

Don't worry about additional parsing, worry about what makes sense to *YOU*,
compiler performance is hardly that important.
 
You don't generally need "this" when you're referring to your own
properties, variables, and other members, but it can help to make the
code more readable, or even to disambiguate if you're unfortunate
enough to have several possibilities with the same name.

As far as I know, the only time when you *really* need to use "this"
is when you aren't accessing any specific member of the current object
but merely the object itself, e.g.

void Print()
{
Console.Write(this);
}

P.
 
In the code example, if I don't use this., how does the compiler know I
want to assign the parameters to the class level vars or just to
themselves?
 
Brett Romero said:
In the code example, if I don't use this., how does the compiler know I
want to assign the parameters to the class level vars or just to
themselves?

You use 'this'. I was responding to your question about needing this if the
fields had been called nn and kk. More commonly you use properties and dont'
run into naming issues.
 
this makes no difference, except to the developer. At compile time, it
becomes that, and is not used any more. Remember that this is programmer
code, not executable code. that, on the other hand, is executable code. You
can't read that, but the computer can't read this. Of course, these are only
words that I wrote this afternoon. Those, on the other hand, are that which
is neither this nor these. And that's that!

And then some.

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
clintonG said:
Why not try the documentation for a change? C# keywords [1].

We get MANY questions in this newsgroup that could easily be answered by looking at the
documentation or googling the topic. We get MANY posters who don't want to even want to ATTEMPT to
figure it out on their own.
We get many posters who don't even think of searching for an answer before asking a common question.

Brett obviously did some testing, came up with a well thought out question.
He thought he knew the answer, but wasn't sure, so...
He wrote up an example that clarifies the issue and he posted it here as a sanity check.

Why would you pick THIS posting to send a snide response to?????

Have you actually read the C# Keywords entry for "this".
It would NOT have answered his question.

Give the guy a break
Bill
 
...
I'd like to know when exactly the this. keyword is needed.
For example:

public class Combination
{
private long n = 0;
private long k = 0;
private long[] data = null;

public Combination(long n, long k)
{
if (n < 0 || k < 0) // normally n >= k
throw new Exception("Negative parameter in constructor");
this.n = n;
this.k = k;
this.data = new long[k];
for (long i = 0; i < k; ++i)
this.data = i;
}
}

In the above code, I can see how this. may differentiate my class
scoped vars vs. parameters. If the class vars would have been nn and
kk instead of n and k, would this. still have been needed?


Not needed, but...
I've always viewed this. as useful for intelliparse but ultimately just
additional parsing. So I remove it.

If there are name conflicts between instance variables on one hand and local
variables on the other hand, you'll need "this" to differentiate between
them.

In such a short example as you provided you can't see the obvious benefit of
using "this", but as a system grows larger, you'll probably need more
descriptive names on both instance variables and local variables.

When that occurs it might be very handy to just use the same names for
variables with the same "meaning" regardless whether it's a local or
instance variable, and voila, the need for "this" occurs...

I wouldn't remove it, as it gives important information for other developers
that might look at your code. With the "this" keyword it's always obvious
that it's referring to an instance variable.

Without it, it may be more difficult to spot in large methods.

There's also other uses of the this keyword, such as when you're passing a
reference to the instance itself:

otherObject.SomeThing(this);

It's also used as a keyword when declaring indexers:

public long this [int index]
{
get
{
return this.data[index];
}
set
{
this.data[index] = value;
}
}

// Bjorn A
 
Back
Top