0% found this document useful (0 votes)
2 views

11)ArrayList

ArrayList is a dynamically-extendable, untyped array in C# that can grow or shrink as elements are added or removed, allowing it to store any type of data due to its object-based storage. It involves boxing and unboxing for value types, which can introduce performance overhead. However, List<T> is generally preferred in modern applications for its type safety and performance advantages.

Uploaded by

Sanjeev Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

11)ArrayList

ArrayList is a dynamically-extendable, untyped array in C# that can grow or shrink as elements are added or removed, allowing it to store any type of data due to its object-based storage. It involves boxing and unboxing for value types, which can introduce performance overhead. However, List<T> is generally preferred in modern applications for its type safety and performance advantages.

Uploaded by

Sanjeev Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

//---------------------------------------------------------------------------- ArrayList -------------------------------------------------------------------------//

// C# has some inbuilt list type data structures – ArrayList is one of them. It is a part of the System.Collections namespace.
// ArrayList is an untyped dynamically-extendable array. Unlike arrays, which have a fixed size, ArrayList can dynamically grow or shrink as
elements are added or removed - implemented similarly to the static list implementation.
// Dynamic : When you add an element, ArrayList checks if the current size equals the capacity. If the size equals the capacity, the ArrayList
needs to resize. It typically doubles its capacity to accommodate new elements, which minimizes the number of resizing operations needed
when adding many elements. This is probably internally done by allocating a new array buffer with a larger capacity (usually double the
current size), copying the elements from the old array buffer to the new one, then replacing the reference to the old array buffer with the
new one.
// Untyped: ArrayList stores its elements in an array (so contiguous memory locations) of type object. Since all types in C# ultimately derive
from the object class, this allows ArrayList to hold any type of data, including value types (like int, double, etc.) and reference types (like
string, custom classes, etc.).
// Boxing, Unboxing : When you add a value type to an ArrayList, it is automatically boxed, which means it is wrapped in an object
reference. This can introduce some overhead (make code slow) due to boxing and unboxing.
// Know that there are two types of storage a process has, the stack and the heap. Value types are typically stored on the stack and
reference types on the heap. Pushing and popping the stack is fast, no garbage collection is required either. For reference types, not only
does memory need to be allocated by the runtime but the GC must be made aware of them as well for managing them.Boxing is used to
store value types in the garbage-collected heap.
// Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value
type allocates an object instance on the heap and copies the value into the new object. Boxing and unboxing are computationally expensive
processes. When a value type is boxed, a new object must be allocated and constructed. Later when you pull it out, the value is copied out
and put on the stack. Boxing is necessary when you need to pass a value type to a method that expects an object reference. For example, if
you want to add an integer value to an ArrayList, which only accepts objects, we need to box the integer value before adding it to the
ArrayList. (done by inbuilt boxing)
Unboxing is necessary when you need to retrieve the original value type from an object reference. For example, if you retrieve an integer
value from an ArrayList, which only stores objects, you need to unbox the object reference before using it as an integer.

static void Main(string[] args)


{
ArrayList list = new ArrayList(); // Instantiates an object of ArrayList using ‘new’ and allocates…
list.Add("Hello"); // … a fixed-size array (the buffer) of default capacity, typically a small size (16) to hold its elements.
list.Add(5); // ArrayList is a non-generic collection i.e, the same ArrayList instance can store elements of different types
list.Add(3.14159); // Float value type is now boxed into an object stored on heap – pointed to only by a reference stored on stack
list.Add(DateTime.Now); // ArrayList has many inbuilt methods : Add , Insert, Count, Remove, RemoveAt,etc
for(int i = 0; i < list.Count ; i++ )
{
object value = list[i]; // ArrayList uses an indexer this[int]
Console.WriteLine(“Index={0}; Value={1}”, i, value);
}
ArrayList newList = new ArrayList(10);
object noble = "Xenon";
newList.Insert(0, "OXygen"); // The index for insertion must be less than or equal to size (not same as capacity )of ArrayList
newList.Add(noble); // We cannot insert at an index greater than size of ArrayList – throws ArgumentOutOfRangeException
newList.Insert(2, 'U'); // The second argument of Insert method must be an object or object-derived
newList.Add(4); // Add appends at the end
newList.Add(5.2f);
newList.Add("Uranium");
newList.Insert(5,"Rupees");
Console.WriteLine(newList[1]);
Console.WriteLine(newList.Count);
int integer = (int)newList[3]; // This is unboxing ; without type cast we cannot assign newList[3] to an int variable
dynamic num = newList[4]; // Dynamic keyword in C# is used to declare variables that can hold any type of data…
dynamic element = newList[5]; // …and its type is resolved at runtime rather than at compile time.
Console.WriteLine((dynamic)newList[3]+num+element); // Operations over dynamic variables (like the + …
//… operator used) are resolved at runtime and their action depends on the actual values of their arguments. This line returns…
// … 9.2Rupees – implying integer & float were added mathematically but then concatenated like a string with “Rupees”.
// Dynamic – Universal Datatype
Console.ReadLine();
}
In modern applications, List<T> is almost always a better choice due to its type safety, performance benefits, and better compile-time
checking. Also, ArrayList is not thread-safe.

You might also like