This project is read-only.
1

Resolved

C# Overload Resolution Regression in VS2015

description

This code (while a little sketch) compiles with VS2013 but on VS2015 produces errors:
1>C:\Users\hmansell\Documents\Visual Studio 14\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs(24,26,24,35): error CS0121: The call is ambiguous between the following methods or properties: 'Foo.IfNotNull<T, U>(T, Func<T, U>, U)' and 'Foo.IfNotNull<T, U>(T?, Func<T, U>)'
1>C:\Users\hmansell\Documents\Visual Studio 14\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs(25,26,25,35): error CS0121: The call is ambiguous between the following methods or properties: 'Foo.IfNotNull<T, U>(T, Func<T, U>, U)' and 'Foo.IfNotNull<T, U>(T?, Func<T, U>)'
using System;

namespace ConsoleApplication2
{
    public static class Foo
    {
        public static U IfNotNull<T, U>(this T value, Func<T, U> selector, U defaultValue = default(U))
        {
            return value != null ? selector(value) : defaultValue;
        }

        public static U IfNotNull<T, U>(this T? source, Func<T, U> selector) where T : struct
        {
            return source.HasValue ? selector(source.Value) : default(U);
        }
    }

    class Program
    {
 
        static void Main(string[] args)
        {
            double? val = null;
            var d1 = val.IfNotNull(v => v / 100);
            var d2 = Foo.IfNotNull(val, v => v / 100);
        }
    }
}

comments

angocke wrote Nov 19, 2014 at 11:04 PM

Fixed in changeset ec3c3222f7530bbbb853a0147917d3a3afed3124