What will be the output of the following C# code?
class list
{
ArrayList array = new ArrayList();
public object this[int index]
{
get
{
if (index < 0 || index >= array.Count)
{
return null;
}
else
{
return (array[index]);
}
}
set
{
array[index] = value;
}
}
public int Count
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
list list1 = new list();
list1[0] = "123";
list1[1] = " abc ";
list1[2] = "xyz";
for (int i = 0; i<=list1.Count; i++)
Console.WriteLine(list1[i]);
Console.ReadLine();
}
}
class list
{
ArrayList array = new ArrayList();
public object this[int index]
{
get
{
if (index < 0 || index >= array.Count)
{
return null;
}
else
{
return (array[index]);
}
}
set
{
array[index] = value;
}
}
public int Count
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
list list1 = new list();
list1[0] = "123";
list1[1] = " abc ";
list1[2] = "xyz";
for (int i = 0; i<=list1.Count; i++)
Console.WriteLine(list1[i]);
Console.ReadLine();
}
}
A. Compile time error
B. Run time error
C. 123, abc, xyz
Answer: Option B
Join The Discussion