69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
|
using System;
|
|||
|
using System.Reflection;
|
|||
|
|
|||
|
namespace ProjectCruiser;
|
|||
|
|
|||
|
// Параметризованный набор объектов
|
|||
|
public class ListGenericObjects<T> : ICollectionGenObj<T>
|
|||
|
where T : class
|
|||
|
{
|
|||
|
// Список объектов, которые храним
|
|||
|
private readonly List<T?> _collection;
|
|||
|
|
|||
|
// Максимально допустимое число объектов в списке
|
|||
|
private int _maxCount;
|
|||
|
public int Count => _collection.Count;
|
|||
|
|
|||
|
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
|
|||
|
|
|||
|
public ListGenericObjects()
|
|||
|
{
|
|||
|
_collection = new();
|
|||
|
}
|
|||
|
|
|||
|
public T? GetItem(int position)
|
|||
|
{
|
|||
|
if (position > _maxCount - 1 || position < 0)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return _collection[position];
|
|||
|
}
|
|||
|
|
|||
|
public int Insert(T obj)
|
|||
|
{
|
|||
|
if (Count + 1 < _maxCount || obj == null)
|
|||
|
{
|
|||
|
return -1;
|
|||
|
}
|
|||
|
_collection.Add(obj);
|
|||
|
return Count;
|
|||
|
}
|
|||
|
|
|||
|
public int Insert(T obj, int position)
|
|||
|
{
|
|||
|
if (position >= _maxCount || position < 0 ||
|
|||
|
_collection[position] != null)
|
|||
|
{
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
_collection.Insert(position, obj);
|
|||
|
return position;
|
|||
|
}
|
|||
|
|
|||
|
public T? Remove(int position)
|
|||
|
{
|
|||
|
if (position >= Count || position < 0)
|
|||
|
// on the other positions items don't exist
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
T? item = _collection[position];
|
|||
|
_collection.RemoveAt(position);
|
|||
|
return item;
|
|||
|
}
|
|||
|
}
|