Resizing an Array Using Array.Resize()

In this example I tried to pass an array of nulls (int? [] ) to a function in order to remove all the null values then pass it back to the nullable array.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int?[] x = new int?[] { 5, null, 3, null, 9, null, null, null, null, 4, null, 3, null, 6 };
            Console.WriteLine("---------------------------Null holder array ");
            foreach (int? element in x)
            {
                if (element.HasValue)
                    Console.WriteLine(element);
                else
                    Console.WriteLine("NULL");
            }
            Console.WriteLine("my array length " + x.Length);
            ////I can use nullfree with out no more resize or copy in x array.
            int?[] nullfree = function(ref x);
            Array.Resize(ref x, nullfree.Length);
            Array.Copy(nullfree, x, nullfree.Length);
            Console.WriteLine("---------------------------New Null free array ");
            foreach (int? element in x)
            {
                Console.WriteLine(element);
            }
            Console.WriteLine("my array new length " + x.Length);
        }
        static int?[] function(ref int?[] x)
        {
            int size = -1;
            int num = 0;
            int?[] nullfree = new int?[x.Length];
            for (int i = 0; i <>(ref nullfree, size + 1);
                return nullfree;
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all