C#
Coded UI Test Automation Framework Tutorial
A typical software automation Testing requires automation tool like Selenium and QTP. Coded UI is...
The ArrayList collection is similar to the Arrays data type in C#. The biggest difference is the dynamic nature of the array list collection.
For arrays, you need to define the number of elements that the array can hold at the time of array declaration. But in the case of the Array List collection, this does not need to be done beforehand. Elements can be added or removed from the Array List collection at any point in time. Let's look at the operations available for the array list collection in more detail.
The declaration of an ArrayList is provided below. An array list is created with the help of the ArrayList Datatype. The "new" keyword is used to create an object of an ArrayList. The object is then assigned to the variable a1. So now the variable a1 will be used to access the different elements of the array list.
ArrayList a1 = new ArrayList()
The add method is used to add an element to the ArrayList. The add method can be used to add any sort of data type element to the array list. So you can add an Integer, or a string, or even a Boolean value to the array list. The general syntax of the addition method is given below
ArrayList.add(element)
Below are some examples of how the "add" method can be used. The add method can be used to add various data types to the Array List collection.
Below you can see examples of how we can add Integer's Strings and even Boolean values to the Array List collection.
Now let's see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.
In the program below, we will write the code to create a new array list. We will also show to add elements and to display the elements of the Array list.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList a1 = new ArrayList();
a1.Add(1);
a1.Add("Example");
a1.Add(true);
Console.WriteLine(a1[0]);
Console.WriteLine(a1[1]);
Console.WriteLine(a1[2]);
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
From the output, you can see that all of the elements from the array list are sent to the console.
Let's look at some more methods which are available as part of the ArrayList.
ArrayList.Count() – This method will return the number of elements that the array list contains.
ArrayList.Contains(element) – This method will return true if the element is present in the list, else it will return false.
ArrayList.RemoveAt(index) – This method will remove an element from a specific position of the Array List.
Now let's see this working at a code level. All of the below-mentioned code will be written to our Console application. The code will be written to our Program.cs file.
In the below program, we will write the code to see how we can use the above-mentioned methods.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList a1 = new ArrayList();
a1.Add(1);
a1.Add("Example");
a1.Add(true);
Console.WriteLine(a1.Count);
Console.WriteLine(a1.Contains(2));
Console.WriteLine(a1[1]);
a1.RemoveAt(1);
Console.WriteLine(a1[1]);
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
Why is the last value true?
If you see the sequence of events, the element Example is removed from the array because this is at position 1. Position 1 of the array then gets replaced by what was in position 2 earlier which the value 'true'
Summary
A typical software automation Testing requires automation tool like Selenium and QTP. Coded UI is...
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...
C# has a wide array of file operations. These operations include opening a file, reading or...
C# is a general purpose programming language which encompasses various disciplines like...
What are Data Types in C#? The C# language comes with a set of Basic data types. These data types are...
C# Enumeration An enumeration is used in any programming language to define a constant set of...