Extension methods conflict
Posted
by
Yochai Timmer
on Stack Overflow
See other posts from Stack Overflow
or by Yochai Timmer
Published on 2011-03-12T15:55:43Z
Indexed on
2011/03/12
16:10 UTC
Read the original article
Hit count: 354
Lets say I have 2 extension methods to string, in 2 different namespaces:
namespace test1
{
public static class MyExtensions
{
public static int TestMethod(this String str)
{
return 1;
}
}
}
namespace test2
{
public static class MyExtensions2
{
public static int TestMethod(this String str)
{
return 2;
}
}
}
These methods are just for example, they don't really do anything.
Now lets consider this piece of code:
using System;
using test1;
using test2;
namespace blah {
public static class Blah {
public Blah() {
string a = "test";
int i = a.TestMethod(); //Which one is chosen ?
}
}
}
I know that only one of the extension methods will be chosen. Which one will it be ? and why ?
How can I choose a certain method from a certain namespace ?
Edit:
Usually I'd use Namespace.ClassNAME.Method()
... But that just beats the whole idea of extension methods. And I don't think you can use Variable.Namespace.Method()
© Stack Overflow or respective owner