Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic constraints with extension methods #21632

Closed
Julien-Mialon opened this issue Aug 20, 2017 · 3 comments
Closed

Generic constraints with extension methods #21632

Julien-Mialon opened this issue Aug 20, 2017 · 3 comments
Assignees
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@Julien-Mialon
Copy link

Version Used:
Visual Studio 15.3
Steps to Reproduce:
Compile the following code

using System;

namespace A
{
    public interface InterfaceA { }

    public static class Extensions
    {
        public static void DoSomething<T>(this T instance) where T : InterfaceA
        {
            Console.WriteLine("Do something on A");
        }
    }
}

namespace B
{
    public interface InterfaceB { }

    public class ClassB : InterfaceB { }

    public static class Extensions
    {
        public static void DoSomething<T>(this T instance) where T : InterfaceB
        {
            Console.WriteLine("Do something on B");
        }
    }
}

namespace Run
{
    using A;
    using B;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            ClassB classB = new ClassB();
            InterfaceB interfaceB = classB;

            classB.DoSomething();
            interfaceB.DoSomething();
        }
    }
}

Expected Behavior:
Does generic constraint shouldn't be taking in account at a moment and discard the not wrong extension method from the candidate list ?

Actual Behavior:
Compiler error

 error CS0121: The call is ambiguous between the following methods or properties: 'A.Extensions.DoSomething<T>(T)' and 'B.Extensions.DoSomething<T>(T)'
@ufcpp
Copy link
Contributor

ufcpp commented Aug 21, 2017

dotnet/csharplang#98

@jaredpar
Copy link
Member

Believe this behavior is "by design" as constraints can't be used to limit our candidate list. Will let @gafter verify.

@gafter
Copy link
Member

gafter commented Aug 24, 2017

This is by design. However, a proposal under consideration (dotnet/csharplang#98, item 2) would fix this.

@gafter gafter closed this as completed Aug 24, 2017
@gafter gafter added the Resolution-By Design The behavior reported in the issue matches the current design label Aug 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

5 participants