The compiler always creates IL that sets the field using the class name, in any case - whether you specify this or not. The this.
is optional unless there is another variable in your scope with the same name as an instance variable, in which case the compiler knows to set the instance variable.
For example, if you have a class in the TestProject namespace named TestClass, and it contains a field named testOne, the following:
TestClass(string value) // Constructor
{
this.testOne = value;
}
Gets compiled into IL like so:
L_0000: ldarg.0
// ... other initialization stuff
L_0004: ldarg.1
L_0005: stfld string TestProject.TestClass::testOne
The instance variable is always set using the full class information, whether or not "this" is specified.
Edit for comments:
In C#, you can always use this in a method as a keyword because the first argument in the argument list is "this", even if its not specified. For example, say we make a method like so:
class Test
{
void TestMethod(Test instance) {
// Do something
}
void CallTestMethod() {
TestMethod(this);
}
When you look at the IL for CallTestMethod, it will look like this:
.method public hidebysig instance void CallTestMethod() cil managed
{
.maxstack 8
L_0000: nop
L_0001: ldarg.0
L_0002: ldarg.0
L_0003: call instance void CSharpConsoleApplication.Test::TestMethod(class CSharpConsoleApplication.Test)
L_0008: nop
L_0009: ret
}
In this case, the compiler loads ldarg.0
onto the stack twice, which becomes the argument passed into TestMethod (it will become its ldarg.1
). Basically, there is always a "this" internally, in any class method.