0

I'm trying to use extension method, but the method is defined twice with same name. Let's say A.Extensions.Ext() and B.Extensions.Ext() I need both references in my class and when trying

using A.Extensions;
using B.Extensions;
class C
{
    public void Main()
    {
        somevalue.Ext();
    }
}

I want to somehow define which method to use and I don't know how to do that. Thanks for help!

  • 3
    You can call the method explicitly, passing the object you want to act on as the first argument. You don't have to use the extension syntax. https://stackoverflow.com/questions/987541/explicitly-use-extension-method – Rup Oct 20 '18 at 10:01
  • See also https://stackoverflow.com/questions/5283583/extension-methods-conflict – Rui Jarimba Oct 20 '18 at 10:02

1 Answers1

1

There is a way to choose an extension method to be used

Assume you have two classes with extension methods for string

public static class Extension1
    {
        public static string GetLowerCaseResult(this string str)
        {
            return str.ToLowerInvariant();
        }
    }

    public static class Extension2
    {
        public static string GetLowerCaseResult(this string str)
        {
            return str.ToLowerInvariant();
        }
    }

to call it in Main method you have to explicitly specify class and method in this way

static void Main(string[] args)
        {
            var str = "QWERTY";
            Extension2.GetLowerCaseResult(str);
            Console.Read();
        }
svoychik
  • 1,188
  • 1
  • 8
  • 19