This is a premium alert message you can set from Layout! Get Now!

C# List & ArrayList

0

C# List & ArrayList


C# List and ArrayList are two commonly used collection classes in C# programming. They are used to store a collection of items, and both offer various methods for manipulating the items stored within them. However, there are several differences between List and ArrayList, and it is important to understand the differences to choose the best collection class for your needs.

List

The two problems of the array are fixed size and fixed type. This fixed-size problem can be solved using a List. Because the size of the List is dynamic.  But it can't solve the fixed-type problem. The "List" is a generic collection class in C#, meaning it is type-safe, and can only store items of a specified type.

Why need a List?

List is needed in C# because it provides a number of benefits over other collection classes, including:
  1. Type Safety: List is a generic collection class, meaning it is type-safe, and can only store items of a specified type. This helps prevent runtime errors and makes code easier to maintain and debug.
  2. Performance: List is generally faster than other collection classes because it does not require type casting when accessing items.
  3. Flexibility: List provides various methods for adding, removing, and manipulating items within the collection, making it a versatile collection class.
  4. Improved Code Readability: The strong typing of List makes code easier to understand and maintain, as the data type of the items stored in the collection is specified explicitly.
To use List you need to use one namespace. The namespace is:
using System.Collections.Generic;

The rule for declaring a List is:
List<dataType> listName = new List<dataType>();
Example:
List<string> names = new List<string>();

Adding value to the List
List<string> names = new List<string>();
         names.Add("Zakir");
         names.Add("Keya");
         names.Add("Zahid");
         names.Add("Jahangir");
         names.Add("Afsheen");

Declaration & value-adding in the same line
List<string> names = new List<string>() { "Zakir", "Keya", "Zahid", "Jahangir", "Afsheen","Rakib","Limon","Pranto" };


Useful methods of the List with example
List<string> names = new List<string>(){ "Zakir","Keya",
                                         "Zahid","Rakib", 
                                         "Limon", "Pranto" };

  • To Add a value
      names.Add("Jahangir Alom");

  • To Remove a value
      names.Remove("Limon");

  • To Remove from a specific Index number
           //To Remove Limon
            names.RemoveAt(4);

  • To Clear all value
      names.Clear();

  • Copy value from List to Array: To do this, the type of all values must be the same.
      string[] namesArray = new string[names.Count];
            names.CopyTo(namesArray);
names.Count = Length of names List.


Iterate over the List

for loop
List<string> names = new List<string>() { "Zakir","Keya",
                                          "Zahid","Rakib",
                                          "Limon", "Pranto" };
for (int i=0; i<names.Count; i++)
    {
       Console.WriteLine(names[i]);
    }

foreach loop
List<string> names = new List<string>() { "Zakir","Keya",
                                          "Zahid","Rakib",
                                          "Limon", "Pranto" };
foreach (string name in names)
 {
    Console.WriteLine(name);
 }

In summary, List is a powerful and efficient collection class in C# that provides type safety, performance, flexibility, and improved code readability compared to other collection classes.


ArrayList

List and ArrayList are almost the same. The main difference is the type is also dynamic in ArrayList. Yes, ArrayList can solve both the fixed-size and fixed-type problems of Array.

ArrayList in C# is used to store a collection of objects in a dynamically resizable array. It allows you to add and remove elements easily, provides built-in functions for searching, sorting, and manipulation of elements, and is commonly used when the size of the collection is not known beforehand or may change during the program's execution.

Namespace of ArrayList
      using System.Collections;

The rule for declaring an ArrayList is:
      ArrayList arrayListName = new ArrayList();

Example:
      ArrayList myArrayList = new ArrayList();

Declaration & value-adding in the same line
 ArrayList myArrayList = new ArrayList() { 10, true, 67.88, "Zakir" };

Useful methods of the ArrayList with example

All methods and ways are the same as the List. You can also search on google for more methods.

Iterate over the ArrayList

for loop
ArrayList myArrayList = new ArrayList() { 10, true, 67.88, "Zakir" };
            
            for (int i = 0; i < myArrayList.Count; i++)
            {
                Console.WriteLine(myArrayList[i]);
            }

foreach loop
ArrayList myArrayList = new ArrayList() { 10, true, 67.88, "Zakir" };
           
            foreach (var item in myArrayList)
            {
                Console.WriteLine(item);
            }

Notice, since there are many types, I wrote "var" without writing any specific type.

Tags

Post a Comment

0 Comments
Post a Comment

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!
To Top