There are ways to use extension methods with signatures of standard methods as extension (without explicit appeal to static class)? For example:
public class Foo
{
public override string ToString()
{
return "Own";
}
}
public static class ExtensionsMethods
{
public static string ToString(this Foo foo)
{
return "Extensions";
}
}
class Program
{
static void Main()
{
var foo = new Foo();
Console.WriteLine(foo.ToString()); // "Own" will be printed
}
}
Can I use Extensions-ToString version without explicit appeal to class ExtensionsMethods
No, you cannot do this. An extension method is not used if a method with the same signature is available:
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.
See Extension Methods (C# Programming Guide) with Binding Extension Methods at Compile Time for details.
There might be inheritance or a decorator as an alternative solution for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With