using System;
using System.Collections;
namespace ЛР4_МДК_ArrayList
{
public class Car
{
public string cars { get; set; }
public Car(string cars)
{
this.cars = cars;
}
}
class Cars : IEnumerable
{
private ArrayList carsList = new ArrayList(); // Переименовал поле, чтобы не путать с типом данных
public void Add(Car c)
{
carsList.Add(c);
}
public int Count
{
get
{
return carsList.Count;
}
}
public void Clear()
{
carsList.Clear();
}
public void RemoveAt(int index)
{
carsList.RemoveAt(index);
}
public bool Contains(Car c)
{
return carsList.Contains(c);
}
public IEnumerator GetEnumerator()
{
return carsList.GetEnumerator();
}
}
internal class Program
{
static void Main(string[] args)
{
Cars cars = new Cars();
cars.Add(new Car("Alpha"));
cars.Add(new Car("RR"));
cars.Add(new Car("117"));
cars.Add(new Car("Nissan"));
Console.WriteLine("We have {0} cars:", cars.Count);
// ИСПРАВЛЕНО: Тип элемента в цикле изменен на Car, и выводится свойство .cars
foreach (Car c in cars)
{
Console.WriteLine(c.cars);
}
cars.RemoveAt(3);
Console.WriteLine("\nWe have {0} cars:", cars.Count);
// ИСПРАВЛЕНО: Тип элемента в цикле изменен на Car
foreach (Car c in cars)
{
Console.WriteLine(c.cars);
}
Car a = new Car("Audi100");
cars.Add(a);
if (cars.Contains(a))
{
Console.WriteLine("\nFound car: " + a.cars);
}
cars.Clear();
Console.WriteLine("\nWe have {0} cars.", cars.Count);
Console.WriteLine("\n--- Testing separate ArrayList ---");
ArrayList ar = new ArrayList();
ar.Add(cars); // Здесь вы добавляете сам объект коллекции Cars
ar.Add(new Car("abc"));
ar.Add("HI");
ar.Add(33);
foreach (object o in ar)
{
Console.WriteLine(o.ToString());
}
Console.ReadLine(); // Чтобы консоль не закрывалась сразу
}
}
}