Advertisement
wingman007

C#_OOP_Features_Manager<T>

Jun 5th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 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 App1b
  8. {
  9.     class Manager<T>
  10.     {
  11.         private const int SIZE = 5;
  12.         private T[] entities;
  13.         private int counter = 0;
  14.  
  15.         public Manager()
  16.             : this(SIZE)
  17.         { }
  18.  
  19.         public Manager(int size)
  20.         {
  21.             this.entities = new T[size];
  22.         }
  23.  
  24.         public void AddEntity(T entity)
  25.         {
  26.             if (counter >= entities.Length) {
  27.                 throw new InvalidOperationException("The stack is full");
  28.             }
  29.             entities[counter] = entity;
  30.             counter++;
  31.         }
  32.  
  33.         public T RemoveEntity()
  34.         {
  35.             if (counter == 0) {
  36.                 throw new InvalidOperationException("The stack is empty");
  37.             }
  38.             T temp = entities[counter - 1];
  39.             entities[counter - 1] = default(T);
  40.             counter--;
  41.             return temp;
  42.         }
  43.  
  44.         public int GetNumberOfEntities()
  45.         {
  46.             return counter;
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement