Conversion of var to class type in C#?

I am new to C# and was going through ArrayList. My question is can we store different datatype - different classes, different structures into ArrayList and access them. I could see that I can add them but I am not sure on how can we retrieve it. I tried retrieving by using the datatype name i.e. class name but I am seeing that the first member data is missing while I am printing the output. What concept am I missing here?

class OSPF_Area
{
    public string AreaId { get; set; }
    public string AreaName { get; set; }
    public int AreaNumberofRoutes { get; set; }
}

class OSPFLinkPacket
{
    public int LinkPacketCounts { get; set; }
    public int NumberOfHelloPacket { get; set; }
    public string LSAType { get; set; }
}

static void Main(string[] args)
{
    OSPF_Area ospfArea1 = new OSPF_Area();
    ospfArea1.AreaId = "0.0.0.1";
    ospfArea1.AreaId = "non-backbone";
    ospfArea1.AreaNumberofRoutes = 14;

    OSPFLinkPacket ospfLink1 =  new OSPFLinkPacket();
    ospfLink1.LinkPacketCounts = 20;
    ospfLink1.LSAType = "Type4";
    ospfLink1.NumberOfHelloPacket = 40;

    ArrayList OSPFInfo = new ArrayList();

    OSPFInfo.Add(ospfLink1);
    OSPFInfo.Add(ospfArea1);

    foreach(var val in OSPFInfo)
    {
        if(val.GetType().Name == "OSPF_Area")
        {
            Convert.ChangeType(val, typeof(OSPF_Area));
            OSPF_Area area = (OSPF_Area)val;
            Console.WriteLine(area.AreaId);
            Console.WriteLine(area.AreaName);
            Console.WriteLine(area.AreaNumberofRoutes);
        }
    }
    Console.ReadLine();
}

The output is:

non-bacbone
14

I am not sure why the area-id didn't get printed.

Jon Skeet
people
quotationmark

Firstly, I'd advise you to not use ArrayList. Use List<T> where you can, although storing different types of objects in a list is a bit of an anti-pattern to start with.

Convert.ChangeType doesn't do anything for you, and you should use is or as. For example:

OSPF_Area area = val as OSPF_Area;
if (area != null)
{
    Console.WriteLine(area.AreaId);
    Console.WriteLine(area.AreaName);
    Console.WriteLine(area.AreaNumberofRoutes);
}

The problem for the output is almost certainly due to the typo in the first lines of Main:

ospfArea1.AreaId = "0.0.0.1";
ospfArea1.AreaId = "non-backbone";

... you're not assigning anything to AreaName.

people

See more on this question at Stackoverflow