c# arraylist

c# arraylist

ArrayList is class who can hold the reference of similar and dissimilar
types of objects. ArrayList class is provided in System.Collections namespace.
The size of ArrayList is dynamic and we can add or remove elements from it.

ArrayList Add method

Below example use two different classes inherited from IPrintName interface.
IPrintName interface has method called Print which will print the name.

using System;
using System.Collections;
namespace ConsoleApplication
{
public interface IPrintName
{
void Print();
}
public class UserName:IPrintName
{
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Print()
{
Console.WriteLine(_name);
}        
}
public class FriendName:IPrintName
{
string _friendName;
public string MyFriendName
{
get { return _friendName; }
set { _friendName = value; }
}
public void Print()
{
Console.WriteLine(_friendName);
}       
}
class Program
{
static void Main(string[] args)
{
UserName u = new UserName();
u.Name = "SAtalaj";
FriendName f = new FriendName();
f.MyFriendName = "Sid";
ArrayList al = new ArrayList();
al.Add(u);
al.Add(f);
foreach (IPrintName p in al)
{
p.Print();
}
Console.ReadLine();
}       
}
}

Description:
We are iterating the ArrayList with IPrintName reference.
al.Add() method of ArrayList instance adds object of UserName and FriendName.
IPrintName interface refers array list items.
Example also demonstrate use of foreach arraylist containing dissimilar type of items.

ArrayList add elements


Adds items at the end of array list.

Code

using System;
using System.Collections;
namespace ConsoleApplication
{  
class Program
{
static void Main(string[] args)
{
ArrayList firstList = new ArrayList();
firstList.Add("One");
firstList.Add("Two");
firstList.Add("Three");
ArrayList secondList = new ArrayList();
secondList.Add("Four");
secondList.Add("Five");
secondList.Add("Six");
firstList.AddRange(secondList);
foreach (string s in firstList)
{
Console.WriteLine(s);
}
Console.ReadKey();
}       
}
}

Output
One
Two
Three
Four
Five
Six

Description
The AddRange method of ArryList instance appends the second list instance of ArrayList.

C# ArrayList Count

using System;
using System.Collections;
namespace ConsoleApplication
{  
class Program
{
static void Main(string[] args)
{
ArrayList firstList = new ArrayList();
firstList.Add("One");
firstList.Add("Two");
firstList.Add("Three");
Console.WriteLine( firstList.Count );            
firstList.Clear();
Console.WriteLine(firstList.Count);        
Console.ReadKey();                              
}               
}
}


Output
3
0

Description:
Count property of ArrayList instance counts the number of actual elements and returns positive
integer value. Clear method of ArrayList instance clears the ArrayList.

c# arraylist contains

Contains Method returns Boolean value based on whether element exist in array list or not.

using System;
using System.Collections;
namespace ConsoleApplication
{
public class UserName 
{
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public class Employee
{
string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
class Program
{
static void Main(string[] args)
{
UserName u1 = new UserName();
u1.Name = "More";
UserName u2 = new UserName();
u2.Name = "Satalaj";
Employee e = new Employee();
e.Name = "Satalaj";
ArrayList firstList = new ArrayList();
firstList.Add(u1);
firstList.Add(u2);
if (firstList.Contains(e))
{
Console.WriteLine("ArrayList contains : " + e.Name);
}
if (firstList.Contains(u1))
{
Console.WriteLine("ArrayList contains : " + u1.Name);
}
Console.ReadKey();
}               
}
}

Output

ArrayList contains : More

Above example shows array list contains the object u1. The employee instance e doesn’t exists in
ArrayList au .

Tags:

C# ArrayList Search using Linq Extension OfType MethodC# Arraylist foreachC# foreach loopc# stringC# DateTime DifferenceC# XML XmlReaderC# Binary To StringC# Regular Expression Get Text Between BracketsC# For LoopReplace string in C#C# while loop

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.