I use Visual Studio 2015 and it is always telling me "Name can be simplified." on all the "this."

So, can it be simplified with out breaking the other systems? I have simplified a few and nothing seems to go wrong, but I just see it a lot. I don't want to get to the point of no return.

Thank You for your time :D
 
this. reflects the current instance.
Ex, in a Dog.cs scripot in the constructor, idf you place:
Hue = 100;
it is the same as doing:
this.Hue = 100;

Furthermore, this is useful if you need to send the current instance reference elsewhere, ex: most gumps will take a "Mobile from" parameter, it is where the "this" comes in action.
SendGump( new TestGump( this ) );

Reference:
https://msdn.microsoft.com/en-us/library/dk1507sz.aspx
 
Code:
this.Property = value;

Is mostly always redundant since it can just be

Code:
Property = value;

The only time using "this" to access a property in the instance would be necessary is if there was a local variable defined with the same name, which should never happen with proper casing rules.

Code:
public int Number;

public void Main()
{
    Number = 10;
    PrintNumbers( 5 );
}

public void PrintNumbers( int Number )
{
   Console.WriteLine( Number ); // prints 5
   Console.WriteLine( this.Number ); // prints 10
}
 
Last edited:
Back