Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class CustomEvent
  5. {
  6.     private readonly List<Action> events;
  7.  
  8.     public CustomEvent(int capacity = 1)
  9.     {
  10.         events = new List<Action>(capacity);
  11.     }
  12.  
  13.     public event Action Event
  14.     {
  15.         add { events.Add(value); }
  16.         remove { events.Remove(value); }
  17.     }
  18.  
  19.     public void Execute()
  20.     {
  21.         for (var i = 0; i < events.Count; ++i)
  22.         {
  23.             if (events[i] != null)
  24.             {
  25.                 events[i]();
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. public class CustomEvent<T>
  32. {
  33.     private readonly List<Action<T>> events;
  34.  
  35.     public CustomEvent(int capacity = 1)
  36.     {
  37.         events = new List<Action<T>>(capacity);
  38.     }
  39.  
  40.     public event Action<T> Event
  41.     {
  42.         add { events.Add(value); }
  43.         remove { events.Remove(value); }
  44.     }
  45.  
  46.     public void Execute(T value)
  47.     {
  48.         for (var i = 0; i < events.Count; ++i)
  49.         {
  50.             if (events[i] != null)
  51.             {
  52.                 events[i](value);
  53.             }
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement