Question: 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 found something that seemed might work [here](https://stackoverflow.com/a/28611320/1814739)

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;
        }
    }
}

```

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

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

Answer: 
This extension method, `ToArrayOrNull`, does not change the type of the contents. `seq` is an `IEnumerable` of `T`. `result` is an array of `T` (which you sometimes return as `null`, but that's still an array of `T`.

If you really want to convert an `IEnumerable<T>` to an `IEnumerable<T?>` (or an array of `T?`), you should put some constraints on the type parameter (it should be `struct`, meaning a value type, one that cannot be null), and convert each item instead of the whole result, something like:

```
    public static T?[] ToArrayOfNullable<T>(this IEnumerable<T> seq)
        where T : struct
    {
        return seq.Select(val => (T?)val).ToArray();
    }

```

Although I would return an `IEnumerable<T?>` here and convert to an array later, to keep this method simple.
How about this using `Cast<>` and a type constraint making `T` a `struct`:

```
public static IEnumerable<T?> ToArrayOrNull<T>(this IEnumerable<T> seq)
    where T : struct
{
    if (seq.Count() == 0)
        return null;

    return seq.Cast<T?>().ToArray();
}

```