• 0
Phoenix Programmer

إكسير إكسير مايكروسوفت دوت نت كود رقم 0004

سؤال

What is the Miracle and the Disaster of the List object in Microsoft Dot Net Programming Code?

          By default arrays in any programming language cannot increase their size by default. Arrays are not linked lists; arrays are continuous memory allocated boxes that are free not reserved by the system.

C# Example:

          int[] numberAray = new int[10];

          As address number example, it will allocate memory boxes from starting memory address (4001) to ending memory address (4010). It is very risky to increase the array size and allocate the memory address (4011) since this location may be already reserved by the system or another application. So, it is a must to reallocate the array with the new size and copy old array data to the new bigger size array memory boxes.

C# List<T> Class: (Microsoft MSDN)

          List object is used to manage the automatic array size increase reallocation and automatic data copy from the old array to the new array then release the old array reservation in memory and return it back to the system to be used by another application or the system.

List Class Properties:

          Capacity: the size of the array used by the List object.

          Count: the number of elements added to the array used by the List object.

          In case the List capacity equals the List count, then List object will reallocate a new array with size = CAPACITY * 2 (Double Size the Old Array) then copy old array data to the new double sized array and release the oldarray to memory. This is a very costly performance operation when it operates too much in the code. So, the best practice is to forecast the List capacity during List object initialization in order to reduce the automaticarray management cost as much as possible.

C# List Disaster Example:

const int MAX_CAPACITY = 1,000,000;

List<int> numbersList = new List<int>();

for ( int i = 0; i < MAX_CAPACITY; i++){

          numbersList.Add(i);

}

 

C# List Miracle Example:

const int MAX_CAPACITY = 1,000,000;

// Starting List Capacity Initialize
List<int> numbersList = new List<int>( MAX_CAPACITY);

for ( int i = 0; i < MAX_CAPACITY; i++){

          numbersList.Add(i);

}

 

Dot Net Code Elixir Number 0004: 
https://goo.gl/CG7wbT

 

Draft 0004 Dot Net Code Elixir E-Book: 
https://goo.gl/HrygWP

0

شارك هذا الرد


رابط المشاركة
شارك الرد من خلال المواقع ادناه

0 إجابة على هذا السؤال .

لاتوجد إجابات على هذا السؤال حتى الآن .

من فضلك سجل دخول لتتمكن من التعليق

ستتمكن من اضافه تعليقات بعد التسجيل



سجل دخولك الان

  • يستعرض القسم حالياً   0 members

    لا يوجد أعضاء مسجلين يشاهدون هذه الصفحة .