I had a discussion in another thread, and found out that class methods takes precedence over extension methods with the same name and parameters. This is good as extension methods won't hijack methods, but assume you have added some extension methods to a third party library:
public class ThirdParty
{
}
public static class ThirdPartyExtensions
{
public static void MyMethod(this ThirdParty test)
{
Console.WriteLine("My extension method");
}
}
Works as expected: ThirdParty.MyMethod -> "My extension method"
But then ThirdParty updates it's library and adds a method exactly like your extension method:
public class ThirdParty
{
public void MyMethod()
{
Console.WriteLine("Third party method");
}
}
public static class ThirdPartyExtensions
{
public static void MyMethod(this ThirdParty test)
{
Console.WriteLine("My extension method");
}
}
ThirdPart.MyMethod -> "Third party method"
Now suddenly code will behave different at runtime as the third party method has "hijacked" your extension method! The compiler doesn't give any warnings.
Is there a way to enable such warnings or otherwise avoid this?