Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing through this within a class

Tags:

c#

.net

Inside my .NET classes I access my properties and members through this. What is the difference if I access them without using this.

public class Test
{
    private string _test;

    public Test()
    {
        this.Test = "test";
        // vs. 
        Test = "test";

        // and
        this._test = "test";
        // vs. 
        _test = "test";
    }

    public string Test { get; set; }
}
like image 704
Konstantin Dinev Avatar asked Aug 23 '26 06:08

Konstantin Dinev


1 Answers

There's no difference for the compiler at all. Use whatever is more readable. I prefer using this to show that this is a field/property instead of a local variable.

like image 70
Tim Schmelter Avatar answered Aug 24 '26 21:08

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!