static void Main(string[] args) { // membuat objek mhs1 Mahasiswa mhs1 = new Mahasiswa(); mhs1.Nim = "1234"; mhs1.NamaMahasiswa = "Erik"; // membuat objek mhs2 Mahasiswa mhs2 = new Mahasiswa(); mhs2.Nim = "1235"; mhs2.NamaMahasiswa = "Wely"; //membuat objek Dosen Dosen dosen1 = new Dosen(); dosen1.Nik = "0043"; dosen1.NamaDosen = "Jhono"; Dosen dosen2 = new Dosen(); dosen2.Nik = "0045"; dosen2.NamaDosen = "Jhoni"; // membuat objek ArrayList ArrayList list = new ArrayList(); // menambah elemen ArrayList list.Add(mhs1); // boxing list.Add(mhs2); list.Add(dosen1); list.Add(dosen2); // mengakses elemen ArrayList Mahasiswa mhs = (Mahasiswa)list[1]; // unboxing Console.WriteLine("{0}, {1}", mhs.Nim, mhs.NamaMahasiswa); Dosen dosen = (Dosen)list[2]; // unboxing Console.WriteLine("{0}, {1}", dosen.Nik, dosen.NamaDosen); Console.ReadKey(); }
static void Main(string[] args) { Mahasiswa mhs1 = new Mahasiswa(); mhs1.Nim = "1234"; mhs1.NamaMahasiswa = "Erick"; Mahasiswa mhs2 = new Mahasiswa(); mhs1.Nim = "1235"; mhs1.NamaMahasiswa = "Wely"; Dosen dosen1 = new Dosen(); mhs1.Nik = "0043"; mhs1.NamaDosen = "Jhono"; Dosen dosen2 = new Dosen(); mhs1.Nik = "0045"; mhs1.NamaDosen = "Jhoni"; ArrayList list = new ArrayList(); list.Add(mhs1); list.Add(mhs2); list.Add(dosen1); list.Add(dosen2); Mahasiswa mhs = (Mahasiswa)list[1]; Console.WriteLine("{0}, {1}", mhs.Nim, mhs.NamaMahasiswa); Dosen dosen = (Dosen)list[2]; Console.WriteLine("{0}, {1}", dosen.Nik, dosen.NamaDosen); Console.ReadKey(); }