public class ArrList { public string msg = "String of ArrList Class"; string[] sl = new string[3]; // Array is of fixed size, Type Safe ArrayList al = new ArrayList(); // Array List size is dynamic , Not Type Safe public void AddItem() { //Permissible members are string only sl[0] = "Hi"; sl[1] = "Hello"; sl[2] = "Welcome"; //Permissible members are of any data type al.Add("String Msg"); //Adding String al.Add(1); //Adding Integer al.Add('c'); //Adding Char ArrList ob = new ArrList(); al.Add(ob); //Adding userdefined class instance } public void ReadItem() { int i; Console.WriteLine("Printing Array"); for (i = 0; i < 3; i++) { Console.Write(sl[i]+"//"); } i = 0; Console.WriteLine("\n"+"Printing ArrayList"); foreach (var itm in al) { Console.Write(itm + "//"); } } }
public class ArrList
{
public string msg = "String of ArrList Class";
string[] sl = new string[3]; // Array is of fixed size, Type Safe
ArrayList al = new ArrayList(); // Array List size is dynamic , Not Type Safe
public void AddItem()
//Permissible members are string only
sl[0] = "Hi";
sl[1] = "Hello";
sl[2] = "Welcome";
//Permissible members are of any data type
al.Add("String Msg"); //Adding String
al.Add(1); //Adding Integer
al.Add('c'); //Adding Char
ArrList ob = new ArrList();
al.Add(ob); //Adding userdefined class instance
}
public void ReadItem()
int i;
Console.WriteLine("Printing Array");
for (i = 0; i < 3; i++)
Console.Write(sl[i]+"//");
i = 0;
Console.WriteLine("\n"+"Printing ArrayList");
foreach (var itm in al)
Console.Write(itm + "//");
Step 3: In main method, use the below code
Console.WriteLine("//**********Array and ArrayList******************"); ArrList myAr = new ArrList(); myAr.AddItem(); myAr.ReadItem();
Console.WriteLine("//**********Array and ArrayList******************");
ArrList myAr = new ArrList();
myAr.AddItem();
myAr.ReadItem();
I hope this article will help to understand the difference.
Balaji M Kundalam edited Revision 5. Comment: Typography - minor correction