如何将int []类型转换为int?[] [英] How to convert type int[] to int?[]

查看:102
本文介绍了如何将int []类型转换为int?[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用linq查询来输出一个int数组.但是我需要将其传递给仅接受int?[]的方法.

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

因此,在研究了将int []转换为int的方法?[]之后,我发现了一些似乎可行的方法在这里

So after searching on ways to convert int[] to int?[] I found something that seemed might work here

下面的代码是一个简化的示例,显示了什么在起作用,什么不起作用.

The following code is a simplified example that shows what is working and not working.

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // working...
            int[] vids1 = new[] { "", "1", "2", "3" }
                .Where(x => !String.IsNullOrWhiteSpace(x))
                .Select(x => Convert.ToInt32(x))
                .ToArray();

            foreach(int i in vids1) 
            {
                System.Diagnostics.Debug.WriteLine(i.ToString());
            }

            // not working...
            int?[] vids2 = new[] { "", "1", "2", "3" }
                .Where(x => !String.IsNullOrWhiteSpace(x))
                .Select(x => Convert.ToInt32(x))
                .ToArrayOrNull();
        }
    }

    public static class IEnumerableExtensions
    {
        public static T?[] ToArrayOrNull<T>(this IEnumerable<T> seq)
        {
            var result = seq.ToArray();

            if (result.Length == 0)
                return null;

            return result;
        }
    }
}

我一直在尝试使用这种扩展方法,试图使其传回int?[]类型,但到目前为止还没有运气.

I've played around with this extension method trying to get it to pass back int?[] type, but no luck as of yet.

如何获取IEnumerable扩展名ToArrayOrNull来传回可为空的类型?

How can I get my IEnumerable extension ToArrayOrNull to pass back nullable types?

推荐答案

请不要先做错事,然后再进行修正.做正确的事情.您知道您需要一个int?[].因此,创建一个int?[].不要先创建int[],然后再对其进行修复.您可以使用它,但是它毫无意义.

Don't do the wrong thing first, and then fix it up afterwards. Do the right thing straigtaway. You know you need an int?[]. So create an int?[]. Don't first create an int[] and then fix it up. You can get it to work, but it's pointlessly complicated.

int?[] vids1 = new[] { "", "1", "2", "3" }
    .Where(x => !String.IsNullOrWhiteSpace(x))
    .Select(x => (int?) Convert.ToInt32(x))
    .ToArray();

我强烈建议不要尝试使您的ToArrayOrNull工作的原因还在于,它甚至与您要说的应该做的事情差得很远.您可以使其工作,但是在开始编码之前,您必须了解应该发生的情况.只要您仍然不需要它,就暂时将其保留,以后再回头时,您可能会突然看到它.

The reason I'm strongly suggesting not even trying to make your ToArrayOrNull work is also because it doesn't even come close to doing what you're saying it should do. You can make it work, but you have to understand what's supposed to be going on before starting on your coding. Leave this for now as long as you don't need it anyway, and when you look back later you'll probably suddenly see it.

这篇关于如何将int []类型转换为int?[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆