Dynamic Array

A dynamic Array : Simple way to allocate memory dynamically to an Array:

Introduction: some play with “Lazy” generic class I find this way to use array with big memory but not initialize first.

Lazy begins an class that its creation is deferred until it is first used. Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements.

namespace ConsoleApplication4

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] Value = new int[1000000]; // Array hold huge size while initialization

 

            ArraySub sub = new ArraySub();

            sub.Add(0);

            sub.Add(0);

            sub.Add(0);

            sub.Add(0);

            sub.Add(0); // ArrayClass holds only allocated variable

 

        }

    }

 

    public class ArrayValue

    {

        public int[] ArrayItems = new int[1000000]; // Initialization of the array

    }

 

    public class ArraySub

    {

        public Lazy<ArrayValue> lazyClass = new Lazy<ArrayValue>(); // Lazy declaration

        int val = 0;

        public void Add(int i)

        {

            lazyClass.Value.ArrayItems[val++] = i; // Added the value to array

        }

 

        public Lazy<ArrayValue> getItem()

        {

            return lazyClass;

        }

    }

}

Ebook Download
View all
Learn
View all