Extension methods work this way.
According to the C # specification ( 7.6.5.2 Extension method invocations ), extension methods are used only if the call has "one of the forms":
expr . identifier ( ) expr . identifier ( args ) expr . identifier < typeargs > ( ) expr . identifier < typeargs > ( args )
This basically means that you should always have something to the left of the "." so that the compiler allows the extension method. Part of expr. must exist (and not be dynamic ) for the compiler to search for an extension method.
For example, here is a small test that demonstrates:
namespace TestNamespace { using System; public static class TextExtension { public static void Print(this Test test) { Console.WriteLine("Found"); } } public class Test { public void Foo() {
Note the compiler error - since we are inside the Test instance, you cannot directly call Print() without using this. . However, we can easily execute test.Print() (or this.Print() inside the test).
source share