Better use this. before the code?

Sometimes I need to go online and find a tutorial for something. I often find that some people put code like this:

this.button1.Text = "Random Text"; 

Then I find code that looks something like this:

 button1.Text = "Random Text"; 

Is it better to use this.whatever or does it not matter?

+4
source share
11 answers

this just to make it clear, in some cases we have to use this :

  • Differences between parameter and local member :

     //local member object item; private void SomeMethod(object item){ this.item = item;//must use this } 
  • Pass the current instance of the class to another method:

     public class SomeClass { private void SomeMethod(SomeClass obj){ //.... } private void AnotherMethod(){ SomeMethod(this);//pass the current instance into SomeMethod //..... } } 
  • Use in extension methods:

     public static class SomeClassExtension { public static void SomeClassMethod(this SomeClass obj){ //use obj as a reference to the object calling this method... } } 
  • A constructor call from another constructor (with a different signature):

     public Form1(string s) : this() {//Call the Form1() before executing other code in Form1(string s) //...... } 
  • Uses for declaring indexers:

     public class SomeClass { //declare an index returning a string public string this[int index] { get {return ...} set { ... } } } 
  • Use auto properties in struct :

     public struct SomeStruct { public object AutoProp1 {get;set;} public object AutoProp2 {get;set;} public SomeStruct() : this() //must use this { AutoProp1 = someObject; AutoProp2 = someObject; } } 
  • Passing the current instance to classes / types based on:

     public class ClassB : ClassC { //... } public class ClassA : ClassB { public ClassA(){ ((ClassC)this).MemberOfClassC ... ;//There might be some member in ClassC //which is overridden in ClassA or ClassB, casting to ClassC can help we invoke the original member instead of the overridden one. } } 

There may be some other uses for this , however I will be updating later if I think about it.

+3
source

It depends. Here is an example class:

 class A { private int count; public A(int count) { this.count = count; } } 

In this case, "this." This is required because it removes the ambiguity of the link to the left of the job. Without this, it’s not clear to you by reading the code whether β€œcount” will refer to a parameter or field. (Is he cleared of the compiler, which must follow the rules.) But in most cases it is solely a matter of preference.

+10
source

Write all your code to emphasize important points for the reader. If you think it is important for the reader to clearly understand that the identifier refers to an instance member, then use this . If you feel that its an insignificant and distracting detail of implementation, do not do this. Use common sense to make your code readable.

+7
source

It doesn't matter, it's a matter of style. I tend to skip this , as this is just extra code for parsing.

The only time this matters is a name conflict between local and instance variables, in which case this can be used to disambiguate between a field and a local variable.

Here is an example of the type of situation in which it matters:

 public class Foo { private string x; public Foo(string x) { // x = x; Assigns local parameter x to x, not what we want this.x = x; // Assigns instance variable x to local parameter x: this disambiguates between the two. } } 
+3
source

An example of using this might be accessing a class variable if you already have a similar variable in scope. Otherwise it is mostly of choice .

Example

 public class Test { public string firstName { get; set; } public void temp(string firstName) { firstName = this.firstName; } } 
+2
source

For fields, the only case where this explicitly required is when there is a name conflict:

 public class Foo { private string bar; public Foo(string bar) { this.bar = bar; } } 

Thus, some underscores will be added:

 public class Foo { private string _bar; public Foo(string bar) { _bar = bar; } } 
+2
source

This usually does not matter. This is the reason you can use this. , - it is explicit to say that you want to refer to a property / field belonging to the current class.

Again, there are not many cases where you may need this, but, for example, you may have a local variable with the same name as the level property / class. Then you can use this. .

For instance:

 class MyClass { string s = "1"; void MyFunction(string s) { //s = local value as passed in to function //this.s = "1" } } 
+2
source

It usually doesn't matter. This keyword "refers to the current instance of the class and is also used as a modifier of the first parameter of the extension method."

Check out this article.

http://msdn.microsoft.com/en-us/library/dk1507sz.aspx

+1
source

As others have already pointed out, it is useful to distinguish a field / property from method variables. Another place where this is required is to call extension methods in the current instance . For example, this.ExtensionMethod(); will work, but not only ExtensionMethod();

In addition, it is a matter of personal choice, some call it superfluous, and some like to use it. It is entirely up to you and your team.

Personally, I like to use this with class members, especially for the Forms method, if you are working on winform code, for example this.Close();

For details on when to use this see When do you use the keyword "his"?

+1
source

in general, it does not matter, but if you pass a variable with the name, say button1, to the method of the class that already has an element called button1, then you will need to eliminate the fact that you really had in mind.

This is probably why people now use this. To explicitly indicate which variable you had in mind, if you use this practice all the time, you will not be mistaken in those few cases when it is important.

Of course, you can make sure that all member variables are uniquely named, for example, with a prefix of type m_ , but these days it does not correspond to the fashion, people prefer to write this.

+1
source

It really depends on the situation.

http://msdn.microsoft.com/en-us/library/dk1507sz(v=vs.80).aspx

  • To qualify members hidden by similar names
  • Passing an object as a parameter to other methods
  • Indexers Announcement
+1
source

Source: https://habr.com/ru/post/1494532/


All Articles