How to convert type int[] to int?[]

前端 未结 5 2059
傲寒
傲寒 2021-01-24 19:16

I\'m using a linq query to output an int array. But I need to pass this into a method that only accepts int?[].

So after searching on ways to convert int[] to int?[] I

5条回答
  •  猫巷女王i
    2021-01-24 20:05

    How about this using Cast<> and a type constraint making T a struct:

    public static IEnumerable ToArrayOrNull(this IEnumerable seq)
        where T : struct
    {
        if (seq.Count() == 0)
            return null;
    
        return seq.Cast().ToArray();
    }
    

提交回复
热议问题