Articles → .NET → Arraylist in C#

Arraylist in C#






What is an arraylist?





Syntax


ArrayList obj = new ArrayList();



Namespace





Example


using System;
using System.Collections;

namespace Test1 {
	class Program {
		static void Main(string[] args) {
			ArrayList obj = new ArrayList();
			obj.Add("test1");
			obj.Add(1);
			obj.Add(2.03);

			Console.WriteLine("Looping using foreach loop");

			foreach(object o in obj) {
				Console.WriteLine(o);
			}

			Console.WriteLine("Looping using for loop");

			for (int i = 0; i < obj.Count; i++) {
				Console.WriteLine(obj[i]);
			}

			Console.ReadLine();

		}
	}
}


  1. While declaring an arraylist, we have not defined the size.
  2. We can add items as objects which means that we can add data of different data types in the same arraylist.
  3. We can loop through an arraylist using for loop and foreach loop.

Output




Picture showing output of arraylist in C#

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, December 14, 2016

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250