tags:

views:

390

answers:

10

Sorry for asking it again, there are already some questions about this keyword. But all of them tell the purpose of 'this'.

When do you use this keyword
C# when to use this keyword
Use of “this” keyword in formal parameters for static methods in C#
Proper usage of “this.” keyword in C#?

My question is when not to use 'this' keyword .
OR
Is it all right to use this keyword always in situation like the code

class RssReader
{
    private XmlTextReader _rssReader;
    private XmlDocument _rssDoc;
    private XmlNodeList _xn;

    protected XmlNodeList Item { get { return _xn; } }
    public int Count { get { return _count; } }

    public bool FetchFeed(String url)
    {
        this._rssReader = new XmlTextReader(url);
        this._rssDoc = new XmlDocument();
        _rssDoc.Load(_rssReader);
        _xn = _rssDoc.SelectNodes("/rss/channel/item");
        _count = _xn.Count;
        return true;
    }
}

here i have not used 'this' with "_xn" and "_count" also not with "_rssDoc.Load(_rssReader);" is it fine? Should i use "this" with all occurrences of class variables within the class?

Edit: Is it useless to use 'this' in a class for its own variables?

+2  A: 

I would try to be consistent, so that people don't get confused into thinking that the few you do the other way (apart from the way you normally pick) have some special significance.

If you don't use the _whatever naming convention for fields, then you should use this.whatever consistently because otherwise there will be problems when constructors take a whatever parameter and try to put in a whatever field.

Doug McClean
+2  A: 

It is fine. Especially since your class doesn't have a base class and the private fields are named appropriately. ReSharper considers this in your case to be redundant.

Yuriy Faktorovich
+11  A: 

My rule of thumb: Never use 'this' when it is redundant. In this case, 'this' is redundant, so I would avoid it. A tool like ReSharper is very good at telling you when this is the case.

Brian Genisio
I have downloaded it but not tried yet. And yes, redundancy looks odd. Can i assume that using this within a class for its variables is useless because it has no effect?
LifeH2O
Yes, that is a proper assumption. If a member car has the same name as a car in your scope, you must use 'this' to disambiguate. You also need it for constructor chaining, extension methods and a few other subtle cases.
Brian Genisio
+1  A: 

You may, but don't need to unless it's a method that takes arguments with the same names as your class vars (to distinguish them).

mingos
+1  A: 

Should i use "this" with all occurrences of class variables within the class?

In your particular case, NO.

Consider however the following example:

class RssReader
{
    private String url;

    public bool FetchFeed (String url)
    {
        new XmlTextReader (url);

        // vs.

        new XmlTextReader (this.url);

        return true;
    }
}

Here you'll need to specify this to access the instance variable that has the same name as the method argument.

Developer Art
A: 

Well, as for me, 'this' looks really redundant when used with names starting with "_". This is absolutely legal in your example though.

n535
+8  A: 

this is almost always optional and does not need to be specified. If you want to be explicit that you are referring to a member, then use this. If you have a naming convention (such as naming all member fields something like _foo), then you really don't need to refer to them like this._foo.

It's a matter of personal taste (no performance penalty), but I find having the explicit this is harder to maintain and adds little value if you have a solid naming convention. Some people will only use this when calling a member method, e.g. this.Foo(_bar) instead of Foo(_bar), but again, I don't personally believe it adds much.

If you're working with existing code, follow the convention there, otherwise, pick whichever makes you the most productive and effective.

Chris Schmich
I got it!! It is really redundant as everyone else told.
LifeH2O
Just to add to this, another good reason for using `this` is if you are passing in a parameter that has the same name as the private field you are setting.
James
+4  A: 

I always use this. I use the same naming convention for local variables and private fields and it makes the code much easier to read because it becomes obvious if the used identifier is a field or local variable.

Further it prevents the introduction of bugs by adding a new local variable that hides a field.

internal sealed class Foo
{
    private Int32 bar = 42;

    private void Bar()
    {
        // Uncommenting the following line will change the
        // semantics of the method and probably introduce
        // a bug.  
        //var bar = 123;

        Console.WriteLine(bar);

        // This statement will not be affected.
        Console.WriteLine(this.bar);
    }
}

This can be avoided by using different naming conventions for fields and local variables but I really dislike underscore prefixed names. The first character of a word is very important for its readability and an underscore is one of the worst possible choices.

Daniel Brückner
This sounds more like idiot-proofing than bug prevention.
Brian Ortiz
Personally I like the underscore because it's a pain to type and looks ugly. It reminds me to encapsulate fields and go through property accessors whenever possible, even within the class. And "this." is just five characters I don't have to type.
TrueWill
+2  A: 

I always use this. to make it clear that I am referring to a class member, not a local variable.

recursive
+1  A: 

there is absolutely no reason not to use this. even redundancy is no reason not to use it, at all. You get the benefit of the intellisense box to safely complete your code and saves your time by selecting the right variable with the down-key and not to maul your keyboard all the time.

Oops