为什么这个测试告诉我该值为空?

Why is this test telling me that the value is null?

我有这个 Zip 方法:

public static List<List<T>> Zip<T>(this List<List<T>> s)
    {
        if (s is null || s.Count == 0)
        {
            throw new ArgumentNullException(nameof(s));
        }
       foreach (var x in s)
           if (x == null)
               throw new ArgumentNullException();
        var count = s[0].Count;

        if (!s.All(x => x.Count == count))
            throw new ArgumentException("Not all sub lists have the same count.");

        var output = new List<List<T>>();

        for (int i = 0; i < count; i++) //guardo la prima lista in s
        {
            var current_merged_list = new List<T>();

            foreach (var sub_list in s)  //per ogni lista in s
            {
                current_merged_list.Add(sub_list[i]);
            }

            output.Add(current_merged_list);
        }

        return output;
    }

我写了这个测试:

[TestCase(6)]
    public void Test3(int approx)
    {
        var abc = new List<string>() {"a", "b", "c"};
        var bca = new List<string>() { "b", "c", "a" };
        var cab = new List<string>() { "c", "a", "b" };
        var abcInf = AbcInf(abc);    //for some reason abcInf is null
        var bcaInf = BcaInf(bca);    //I guess this is null too
        var cabInf = CabInf(cab);    //I guess this is null too
        var dataList = new List<List<string>>();
        dataList.Add((abcInf as List<string>)); 
        dataList.Add((bcaInf as List<string>));
        dataList.Add((cabInf as List<string>));
        var actual = Check_Res_Approx(dataList.Zip(),approx);  
         var expected = ReturnResult(approx); 
         Assert.AreEqual(actual,expected);
    }
public IEnumerable<string> AbcInf(List<string> abc)
    {
        int i = 0;
        while (true)
        {
            var res = i;
            abc.Add("a");
            abc.Add("b");
            abc.Add("c");
            i++;
            yield return abc[res];

        }
    }

    public IEnumerable<string> BcaInf(List<string> bca)
    {
        int i = 0;
        while (true)
        {
            var res = i;
            bca.Add("b");
            bca.Add("c");
            bca.Add("a");
            i++;
            yield return bca[res];

        }
    }

    public IEnumerable<string> CabInf(List<string> cab)
    {
        int i = 0;
        while (true)
        {
            var res = i;
            cab.Add("c");
            cab.Add("a");
            cab.Add("b");
            i++;

            yield return cab[res];

        }
    }

    public IEnumerable<IEnumerable<string>> Check_Res_Approx(List<List<string>> dataList, int approx)
    {
        int i = 0;
        while (i<approx)
        {
            yield return dataList[i];
        }
    }

    public IEnumerable<IEnumerable<string>> ReturnResult(int approx)
    {
        var abc = new List<string>()
        {
            "a",
            "b",
            "c"
        };
        var bca = new List<string>()
        {
            "b",
            "c",
            "a"
        };
        var cab = new List<string>()
        {
            "c",
            "a",
            "b"
        };
        var actual = new List<List<string>>();
        int i = 0;
        while (true)
        {
            actual.Add(abc);
            actual.Add(bca);
            actual.Add(cab);
            if (i < approx)
                yield return actual[i];
            yield break;
        }
    }

我知道这很混乱,但我的问题不是关于测试本身。 我的问题是为什么它说 List abcInf 为空?我使用方法 AbcInf(abc) 填充它,所以我认为它应该包含无限序列 [ a , b , c , a , b , c , a , b , c , ...] 但显然它没有。

abcInf 不是 null,也不是 List。这是一个 IEnumerable<string>。问题是您试图将其转换为 List<string>,这是完全不同的类型。由于无法进行此转换,因此 abcInf as List<string> 的结果为 null,如 C# documentation.

中所述

如果你想从一个IEnumerable<T>构造一个List<T>,你必须调用一个构造函数,像这样:new List<string>(abcInf)。但是,由于 abcInf 在这种情况下是无限的,构造函数永远不会 return.