4 ms·
> One other advantage to the "this.Foo()" syntax is that this allows using the extension method "Foo", which otherwise could not be resolved by the compiler. I
by cgrealy 6y ago
> One other advantage to the "this.Foo()" syntax is that this allows using the extension method "Foo", which otherwise could not be resolved by the compiler.
I’d argue that calling an extension method of the class from within the class is a good indicator that said extension method should be added as a member... it’s not really an “extension” if it’s required for the class to compile.
But it’s nothing I’d lose sleep over...
- wvenable 6y agoThe extension method might be on an interface of the class and not on the class type itself. That's really the only time I've ever had to use this.
- cgrealy 6y agoFair point.
- ThePadawan 6y agoNote that this is also how can "cheat" to get default but overrideable interface methods (before they were introduced in C# 8): https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUHqAQwFsBTAZwAdCBjUgAgGUB7MgYQnOFYEsAvQgCMANqQAyPQWEJgAnngDeeeivqoAzPR5RgpMADNaDFmQCSOvYbrLVS3Kodad9AEIz6C+gHNSwANz0AL42jk7A9ABizMwAFACUfiFBSUkaapgAbGro9AAipIYQwsAAogAeulDkPMxVSXahKmmomWH0ALKEXjw0McAAFjzkTKyk5roGRvRlcUkODY0OqADs0wB0bmCJ9o3BOyp7ewQkFNR09BJSMrIA8lCkiqkADCPsnNzE/EKil9Jy2w4kgB6IH0ACqUBorDIzgGQ3o3HozAAbnowDw4AwBgxMYVimFLFMyANmHB6AB3HgDeiyZgQMD0GjvXgCYA1KprYEAKlSmhaWQwHVu5Kg5Uq1Vq5Hqc1UzVa2nCnW6vThwxMYwsk3OMxlKgWiyaq3VHC4LO+4kkf1ka3yeNKFVIVXZ5DWSp6MRm9C59HQAN2ST2Di5QJS+zUfJy6tMxEownoIFeGomVgeYf1jjSCtc7gAvAA+egAVj9Kl14baUViszD8zLS1WqtdXXdCTLgdUhzwxzIVCm7Q8qXQmnTS3QORHjRaAE4YvdyRdLdc7qQ1lGY8J4mtK/FWzWOwG8IEgA=== https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUHqAQwFs...
- pmiller2 6y agoIIRC (and, it has been a long time so please be gentle with the down arrow if I'm wrong, lol), you can write extension methods in native code that you might want to call from within the class.