Skip to main content

How to Use the Array List Class in C# by RathrolaPremKumar

In this article, let see uses of Array list classes in C#.

After reading this article, you will learn about,
1)    ArrayList
2)    Uses of ArrayList
3)    How to Access elements in ArrayList
4)    Types of used in  Methods in ArrayList
First and foremost thing have to remember about array is, it does not have a fixed size; it grows as needed always.
Use Add (object) to add an object to the end of the Array List.
Use [] to access the elements in the ArrayList.
Use TrimToSize() to reduce the size to fit the number of elements in the Array List.

User Clear to remove all the elements.
Actually, ArrayList can solves the main disadvantage of an array, which is that we must know the capacity of the data structure when u instantiate the array by providing a data structure that behaves like an array but can grow as required.
As elements are added to an ArrayList object, the capacity is automatically increased. An ArrayList object initially allocates 16 elements. When we add a seventeenth element, the ArrayList expands to 32 elements.
How to Access Elements in an ArrayList
We can access elements in an ArrayList object in the same way that you access arrays. We can also use ArrayList methods to add elements to or remove elements from an ArrayList.
To decrease the capacity of an ArrayList, we can call TrimToSize method.
Use Add method to add items to an ArrayList.
We can also do Foreach to iterate over items in an ArrayList.
ArrayList elements are objects, such as System. Object. So when we retrieve the elements form the list, we must do some Conversions.
List of Methods Used in Array List
            Add an Object to the end of the ArrayList.
            Remove the first appeared object from the ArrayList.
            Removes all elements from the ArrayList.
            Inserts an element into the ArrayList at the specified index.

            Sets the Capacity to the actual number of elements in the ArrayList.
            Sorts the elements in the ArrayList.
            Reverse the elements in the ArrayList.

The following sample code show how to use an ArrayList,
1)    We must include the system.collection namespace as shown below
using System;
using System.Collections;
2)    The ArrayList is initialized without specifying the size, because it may grow when needed.
Name of Arraylist is theAnimals as shown below,
public class Zoo
        private ArrayList theAnimals;
        public ArrayList ZooAnimals
                return theAnimals;

        public Animal this[int i]
                return (Animal) theAnimals[i];
            set {
                theAnimals[i] = value;
        public Zoo()
            theAnimals = new ArrayList();
3)    The Add and Insert methods are used to add elements to the array. This is main difference between Array and ArrayList, as below
public class Guardian
        static void Main(string[] args)
            Zoo myzoo = new Zoo();
            myzoo.ZooAnimals.Add(new Lion());
            myzoo.ZooAnimals.Add(new Elephant());
            myzoo.ZooAnimals.Insert(1, new Lion());

            Animal a = myzoo [0];
            myzoo[1] = new Tiger();


4)    We can access the elements of the ArrayList by using the Index Operator[] as below,
  Animal a = myzoo[0];
  myzoo [1] = new Tiger();

Thanks for reading my article, Hope you like it J


Popular posts from this blog

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation
dont get panic if u get the following error while in your joinings, there is a simple way to solve this.
it is because of collation settings on two columns we are doing joinings. it is because of collation settings on two columns we are doing joinings
First step is to figure out what are two colaltion that cause for conflicts. Let us assume that collation has not been set at the column level and instead at db level, then we have to execute two simple statements as below.

Statements: Select DATABASEPROPERTYYEX('DB1',N'Collation') Select DATABASEPROPERTYYEX('DB2',N'Collation')
One more thing, make a note that if you are on sharepoint you will get error as below
Latin_General_CI_AS_KS_WS. if you are on most any other database and use the dafault settings you may get this SQL_Latin_General_CP1_CI_AS.
Now we have to do some…

How To Create The Data Adapter Object Using ADO.NET by #RathrolaPremKumar

By reading this article, you will learn key points in ADO.NET, as given below. What is Data Adapter?Primary Data Adapters for the databases.Data Adapter properties.Methods used by Data Adapter.How to create Data Adapter with an example? Let’s start our demo with a Data Adapter introduction. Actually, we use Data Adapter object to establish the connection to the data source and manage the movement of date to and from the database. What is Data Adapter? A data adapter aobject serves as a bridge between a data set object and Data Source such as a database to retrieve and save the data. Data adapter contains a set of database commands and a database connection, which we use to fill a dataset object and update the Data Source. Primary data adapters for databases? .NET makes two primary data adapters available for use with the databases. Other data adapters can also be integrated with Visual Studio .NET. Primary Data Adapters are mentioned below. OleDbData Adapter, which is suitable for use wi…

'#Resolve' To export a list, you must have a Microsoft SharePoint Foundation-compatible application.

Searching this error message easily finds the root of the problem. There is a problem with the Office 64 bit version. It doesn’t work with SharePoint. These solutions generally tell you to uninstall your 64 bit office and install a 32 bit version. Happily there is a much easier solution.
Despite the error message the export actually works! Notice the yellow bar at the bottom of your browser. Simply save the .iqy file normally and open it with Excel. It’s that simple. The error message is certainly a nuisance but does not actually stop the export.
Thanks for ready my blog :)