There's not much to it really. You can do it, and you can avoid it. I think it's quite obvious when you're in the getter/setter that you're talking about the object you're in, so I've never used it there.
Also, it seems like Resharper will suggest it's redundant, and gray it out.
If you find it to be of use for you (readability wise), by all means, use it. Otherwise, it'll save you five keystrokes (about a second?) every time you implement a getter by hand ... :)
manpreet
Best Answer
2 years ago
Short and sweet (hopefully), is there a specific reason not to use the
this
keyword when writing getters and setters in C#? I know the typical format, and the one I've always used, is:Recently though, I've been learning Java, and in several of the books I've been using I've seen it written instead like this:
So basically, is there a reason to avoid doing it the same way in C#? Will it cause any problems or errors, or is it a valid approach and really just a matter of personal preference. I'm wondering because, while I know the
this
in C# is understood, explicitly using thethis
keyword seems like it would aid in eliminating a bit extra ambiguity, which personally is always a good thing.Thank you!