473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is this. needed?

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] = 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

Nov 17 '05 #1
8 1397

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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] = 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.
Nov 17 '05 #2
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.
Nov 17 '05 #3
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?

Nov 17 '05 #4
Why not try the documentation for a change? C# keywords [1].

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
[1]
http://msdn.microsoft.com/library/de...eywords_pg.asp
"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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] = 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

Nov 17 '05 #5

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #6
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.

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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] = 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

Nov 17 '05 #7
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
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
Nov 17 '05 #8

"Brett Romero" wrote...
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] = 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
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Positive Contrarian | last post by:
Am looking for a sort of a forum. What's different is that all postings would have one or more addressees, and postings wd be visible only to them and the originator. Unthreaded is OK. (No, mail...
13
by: Joe Feldman | last post by:
This position is located in the South Bay Area in Northern California. If you are interested please send me your resume in a word .doc so that I can review it. If this does not look like a match,...
3
by: JD | last post by:
I have a web site built with asp.net and I have been told that the only files I need on the webserver are the files in the bin folder and aspx files, I don't need the .vb or the .resx files on the...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.