wingman007

2015_OOP_EnumNestedGeneric_Manager<T>

May 20th, 2015
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace World1
  8. {
  9.     // read more about the Constraints https://msdn.microsoft.com/en-us/library/d5x73970.aspx
  10.     class Manager<T> //we can have Constraints (e.g. where T : class)
  11.     {
  12.         private const int CAPACITY = 5;
  13.  
  14.         private T[] entities;
  15.  
  16.         private int counter = 0;
  17.  
  18.         public T this[int i]
  19.         {
  20.             get { return entities[i]; }
  21.             set { entities[i] = value; }
  22.         }
  23.  
  24.         public Manager()
  25.             : this(CAPACITY)
  26.         { }
  27.         public Manager(int capacity)
  28.         {
  29.             entities = new T[capacity];
  30.         }
  31.  
  32.         public void AddEntity(T entity) // Push
  33.         {
  34.             if (counter >= entities.Length)
  35.             {
  36.                 throw new InvalidOperationException("The stack is full!");
  37.             }
  38.             this.entities[counter] = entity;
  39.             counter++;
  40.         }
  41.  
  42.         public T RemoveEntity() // <K>
  43.         {
  44.             if (counter == 0) {
  45.                 throw new InvalidOperationException("The stack is empty!");
  46.             }
  47.             T temp = entities[counter - 1];
  48.             entities[counter - 1] = default(T);
  49.             counter--;
  50.             return temp;
  51.         }
  52.  
  53.         public int GetNumberOfEntities()
  54.         {
  55.             return counter;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment