Why do some use `this.`?

Possible duplicate:
What is the purpose of this keyword in C #.

Hello,

I have a question about something. I looked around, but I can not understand.



Why do some programmers use this before something? How:

 this.button1 = String.Empty; 

On MSDN, I never remember that this. used this. if this. did not refer to the Form itself, for example:

 this.WindowState = FormWindowState.Minimized; 

Is this how we really should refer to things? Or are there any additional benefits to this? So far, I have not had any noticeable advantages; I have not noticed any changes.


Thanks: -)

+4
source share
12 answers

the this keyword is often used as a way to be explicit in which a variable occurs. For example, a large function can have many variables, and use of this can be used to determine the true properties for a given class and variable functions.

Also consider the example below, where you need to use a keyword to distinguish between a class variable and a function parameter.

 object one; object two; public MyClass(object one, object two) { this.one = one; this.two = two; } 
+10
source

Actually, you use this to refer to the container object. Using this is sometimes useful in solving some cases of addition as follows:

 public class Person { private String name; public Person(String name) { this.name = name; } } 

However, you can avoid using this by changing the name of the private field / variable:

 public class Person { private String _name; public Person(String name) { _name = name; } } 
+8
source

This means that this object: if you are in form, in form, if you are in a simple class, this is your class. We should not use it (it is implicit), but sometimes you need it to make the code very clear or necessary when:

 class MyClass { private int tot = 0; public MyClass(int tot) { this.tot = tot; } } 
+6
source

Take a look at http://msdn.microsoft.com/en-us/library/dk1507sz(v=vs.71).aspx . We mainly use it to ensure that we mean class members that can be hidden. MSDN example:

 public Employee(string name, string alias) { this.name = name; this.alias = alias; } 
+6
source

Using the this in your code will distinguish between the areas in which the properties and methods of the class you are currently in are located. It also means that we use instance methods and properties, not static methods and properties.

It also means that we must first create it.

Therefore, it will improve the readability and meaning of your code, especially in a large team of software developers, where the code will be transferred to other users.

+5
source

Something like this.button1 needed if another button1 in a more local area than your object.

+4
source

Designation this. Systematically used by the Windows Form Generator, possibly where you saw it.

this. used to disambiguate if the name of the member variable matches the method parameter, for example.

It’s also a matter of taste, but I think most programmers don’t use it because it takes a lot of time (but a little more readable, I would say).

+4
source

Remember that with this. sometimes a must. As an example, when assigning field values ​​with the same name as constructor arguments, for example

 public class MyClass{ private string name; public MyClass(string name) { this.name = name; // <== This is mandatory } } 
+4
source

the same for the compiler. The form has controls as fields, and adding the this simply returns to the reader that we are using form fields

+3
source

This is not necessary (the code will work without it), however, it makes it more clear where the method or field is located. If you use StyleCop on the most picky settings, Microsoft recommends this style.

Edit: Actually, most of the time this is optional, but as mentioned in several other answers, sometimes conflict resolution is required.

+3
source
 SomeNameSpace.SomeClass.SomeMethod() SomeNameSpace.SomeClass.OtherMethod() 

when you are in SomeMethod() , say you want to call OtherMethod() . Using this prevents you from writing SomeNameSpace.SomeClass.OtherMethod() every time you want to use it.

Since you are already in SomeClass , this will reference SomeClass when you work inside SomeMethod() . So, all you have to do for the OtherMethod() link is go: this.OtherMethod()

This is a good shortcut that will also make your programs easier in the future if you need to change SomeNameSpace or SomeClass to other names .. this will still work the way you intended.

 namespace Foo { public class Bar { public void Method() { // Do Other Stuff } public void OtherMethod() { // Do Some Stuff this.Method(); // Do Other Stuff // Instead of Foo.Bar.Method() } } } 
+3
source

This is not necessary, but I believe that the main reason for following this practice is to improve readability. As an example, it is very easy for the reader to distinguish between local variables and method parameters.

 public class User { string firstname; string lastname; public User(string firstname, string lastname) { this.firstname = firstname; this.lastname = lastname; } } 
+1
source

All Articles