What is the purpose of ArrayList in .NET?

ArrayList is used to store elements of different data types.



Advantage of ArrayList is that its size can grow or shrink dynamically. Here is an example using an ArrayList to store and read list of integers and list of strings:

public class sampleClass {
public static void displayList(ArrayList sampleList) {
foreach(var element in sampleList) {
Console.Write(“ “ + element);
}
}
public static void Main() {
ArrayList intList = new ArrayList();
intList.Add(10);
intList.Add(20);
Console.WriteLine(“Elements of integer list are:”);
displayList(intList);
ArrayList stringList = new ArrayList();
stringList.Add(“Hello!”);
stringList.Add(“Have a Nice Day!”);
Console.WriteLine(“Elements of string list are:”);
displayList(stringList);
}
}

Output of this code will be:

Elements of integer list are: 10 20
Elements of string list are: Hello! Have a Nice Day!

ArrayList has many methods like Clear(), AddRange(<list>), RemoveAt(<index>), Insert(<position, element>) and properties like count. You can use them to manipulate on ArrayList elements.

| What is Private Access Modifier in C#? | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.