Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace World1
- {
- // read more about the Constraints https://msdn.microsoft.com/en-us/library/d5x73970.aspx
- class Manager<T> //we can have Constraints (e.g. where T : class)
- {
- private const int CAPACITY = 5;
- private T[] entities;
- private int counter = 0;
- public T this[int i]
- {
- get { return entities[i]; }
- set { entities[i] = value; }
- }
- public Manager()
- : this(CAPACITY)
- { }
- public Manager(int capacity)
- {
- entities = new T[capacity];
- }
- public void AddEntity(T entity) // Push
- {
- if (counter >= entities.Length)
- {
- throw new InvalidOperationException("The stack is full!");
- }
- this.entities[counter] = entity;
- counter++;
- }
- public T RemoveEntity() // <K>
- {
- if (counter == 0) {
- throw new InvalidOperationException("The stack is empty!");
- }
- T temp = entities[counter - 1];
- entities[counter - 1] = default(T);
- counter--;
- return temp;
- }
- public int GetNumberOfEntities()
- {
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment