SIGN UP MEMBER LOGIN:    
ARTICLE

Resizing an Array Using Array.Resize()

Posted by mahmoud mahmoud Articles | How do I July 05, 2010
In this article we will see how to resize an array using Array.Resize().
Reader Level:

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

Login to add your contents and source code to this article
Article Extensions
Contents added by mahmoud mahmoud on Jul 05, 2010
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 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<int?>(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 < x.Length; i++)
            {
                if (x[i].HasValue)
                {
                    nullfree[num] = x[i].Value;
                    num++;
                    size++;
                }
            }
            Array.Resize<int?>(ref nullfree, size + 1);
            return nullfree;
        }
    }
}

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor