MSBuild15 CS0121 不明确 'List<T>.List(params T[])' 和 'List<T>.List(params List<T>[])'

MSBuild15 CS0121 ambiguous 'List<T>.List(params T[])' and 'List<T>.List(params List<T>[])'

以下代码将在 MSBuild12 中编译,但在 MSBuild15 中将失败

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Example
{
    public class List<T> : IEnumerable<T>
    {
        private readonly T[] _items;

        public List(params T[] items)
        {
            _items = items;
        }

        public List(params List<T>[] args)
        {
            _items = args.SelectMany(t => t).ToArray();
        }

        public static List<T> New
        {
            get { return new List<T>(); }
        }


        public IEnumerator<T> GetEnumerator()
        {
            foreach (var item in _items)
                yield return item;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }
    }
}

结果

CS0121 The call is ambiguous between the following methods or properties: 'List.List(params T[])' and 'List.List(params List[])' Example D:\Example\Test.cs

在 MSBuild 12 中,此构造函数默认为。

问题出在以下行:

public static List<T> New
{
    get { return new List<T>(); }
}

这是因为不清楚应该使用两个构造函数中的哪一个。您可以通过提供适当的参数指定要使用的两者之一来克服此问题:

public static List<T> New => new List<T>(new T[]{});

public static List<T> New => new List<T>(new List<T>{});

另一种方法是声明无参数构造函数

public List()
{ 
    _items = new T[0];
}

然后有这个:

public static List<T> New => new List<T>();