When do you use the “this” keyword? [closed]

2018-12-31 02:13发布

I was curious about how other people use the this keyword. I tend to use it in constructors, but I may also use it throughout the class in other methods. Some examples:

In a constructor:

public Light(Vector v)
{
    this.dir = new Vector(v);
}

Elsewhere

public void SomeMethod()
{
    Vector vec = new Vector();
    double d = (vec * vec) - (this.radius * this.radius);
}

30条回答
无与为乐者.
2楼-- · 2018-12-31 02:25

I use it every time I can. I believe it makes the code more readable, and more readable code equals less bugs and more maintainability.

查看更多
君临天下
3楼-- · 2018-12-31 02:25

When you are many developers working on the same code base, you need some code guidelines/rules. Where I work we've desided to use 'this' on fields, properties and events.

To me it makes good sense to do it like this, it makes the code easier to read when you differentiate between class-variables and method-variables.

查看更多
孤独总比滥情好
4楼-- · 2018-12-31 02:27

Another somewhat rare use for the this keyword is when you need to invoke an explicit interface implementation from within the implementing class. Here's a contrived example:

class Example : ICloneable
{
    private void CallClone()
    {
        object clone = ((ICloneable)this).Clone();
    }

    object ICloneable.Clone()
    {
        throw new NotImplementedException();
    }
}
查看更多
只靠听说
5楼-- · 2018-12-31 02:28

this on a C++ compiler

The C++ compiler will silently lookup for a symbol if it does not find it immediately. Sometimes, most of the time, it is good:

  • using the mother class' method if you did not overloaded it in the child class.
  • promoting a value of a type into another type

But sometimes, You just don't want the compiler to guess. You want the compiler to pick-up the right symbol and not another.

For me, those times are when, within a method, I want to access to a member method or member variable. I just don't want some random symbol picked up just because I wrote printf instead of print. this->printf would not have compiled.

The point is that, with C legacy libraries (§), legacy code written years ago (§§), or whatever could happen in a language where copy/pasting is an obsolete but still active feature, sometimes, telling the compiler to not play wits is a great idea.

These are the reasons I use this.

(§) it's still a kind of mystery to me, but I now wonder if the fact you include the <windows.h> header in your source, is the reason all the legacy C libraries symbols will pollute your global namespace

(§§) realizing that "you need to include a header, but that including this header will break your code because it uses some dumb macro with a generic name" is one of those russian roulette moments of a coder's life

查看更多
皆成旧梦
6楼-- · 2018-12-31 02:29

There are several usages of this keyword in C#.

  1. To qualify members hidden by similar name
  2. To have an object pass itself as a parameter to other methods
  3. To have an object return itself from a method
  4. To declare indexers
  5. To declare extension methods
  6. To pass parameters between constructors
  7. To internally reassign value type (struct) value.
  8. To invoke an extension method on the current instance
  9. To cast itself to another type
  10. To chain constructors defined in the same class

You can avoid the first usage by not having member and local variables with the same name in scope, for example by following common naming conventions and using properties (Pascal case) instead of fields (camel case) to avoid colliding with local variables (also camel case). In C# 3.0 fields can be converted to properties easily by using auto-implemented properties.

查看更多
听够珍惜
7楼-- · 2018-12-31 02:31

Personally, I try to always use this when referring to member variables. It helps clarify the code and make it more readable. Even if there is no ambiguity, someone reading through my code for the first time doesn't know that, but if they see this used consistently, they will know if they are looking at a member variable or not.

查看更多
登录 后发表回答