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 ?
}
}
}
The Question:
I know that only one of the extension methods will be chosen.
Which one will it be ? and why ?
Edit:
This also bothers me, but not as much because it's a static method in a static class after all:
How can I choose a certain method from a certain namespace ?
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()
test1.MyExtensions.TestMethod(a)
if in doubt instead ofa.TestMethod()
– Cephaloniatest1.MyExtensions.TestMethod(a)
beats the idea of the extension methods. I've edited my question, that's not really what's bothering me. – Loggiausing static
to specify the class for the extension you want to use - riptutorial.com/csharp/example/34/… – Memento