c # Добавление метода Remove (int index) в класс очереди .NET
Объединение предложений CasperOne c-sharp и Дэвида Андерсона на новый c#-language уровень. Следующий класс csharp наследуется от List и скрывает visual-c# методы, которые могут нанести queue ущерб концепции FIFO при c# добавлении трех методов Queue c#.net (Equeue, Dequeu, Peek).
public class ListQueue : List
{
new public void Add(T item) { throw new NotSupportedException(); }
new public void AddRange(IEnumerable collection) { throw new NotSupportedException(); }
new public void Insert(int index, T item) { throw new NotSupportedException(); }
new public void InsertRange(int index, IEnumerable collection) { throw new NotSupportedException(); }
new public void Reverse() { throw new NotSupportedException(); }
new public void Reverse(int index, int count) { throw new NotSupportedException(); }
new public void Sort() { throw new NotSupportedException(); }
new public void Sort(Comparison comparison) { throw new NotSupportedException(); }
new public void Sort(IComparer comparer) { throw new NotSupportedException(); }
new public void Sort(int index, int count, IComparer comparer) { throw new NotSupportedException(); }
public void Enqueue(T item)
{
base.Add(item);
}
public T Dequeue()
{
var t = base[0];
base.RemoveAt(0);
return t;
}
public T Peek()
{
return base[0];
}
}
Тестовый код:
class Program
{
static void Main(string[] args)
{
ListQueue queue = new ListQueue();
Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
Console.WriteLine();
for (int i = 1; i <= 10; i++)
{
var text = String.Format("Test{0}", i);
queue.Enqueue(text);
Console.WriteLine("Just enqueued: {0}", text);
}
Console.WriteLine();
Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
Console.WriteLine();
var peekText = queue.Peek();
Console.WriteLine("Just peeked at: {0}", peekText);
Console.WriteLine();
var textToRemove = "Test5";
queue.Remove(textToRemove);
Console.WriteLine("Just removed: {0}", textToRemove);
Console.WriteLine();
var queueCount = queue.Count;
for (int i = 0; i < queueCount; i++)
{
var text = queue.Dequeue();
Console.WriteLine("Just dequeued: {0}", text);
}
Console.WriteLine();
Console.WriteLine("Item count in ListQueue: {0}", queue.Count);
Console.WriteLine();
Console.WriteLine("Now try to ADD an item...should cause an exception.");
queue.Add("shouldFail");
}
}
c#
queue
2021-12-15T01:08:57+00:00
2022-11-10T10:00:24+00:00
Azanelka
Вопросы с похожей тематикой, как у вопроса:
c # Добавление метода Remove (int index) в класс очереди .NET
Предупреждение о файлах Cookies
Мы используем файлы cookies для улучшения работы сайта. Оставаясь на нашем сайте, вы соглашаетесь с условиями использования файлов cookies. Чтобы ознакомиться с нашими Положениями о конфиденциальности и об использовании файлов cookie, нажмите здесь.