This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Cyclic Immutable List

By: a guest on Feb 14th, 2013  |  syntax: C#  |  size: 0.90 KB  |  views: 1  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             TasksList tl = new TasksList(new int[] { 1, 2, 3, 4, 5 });
  6.             Console.WriteLine("The tasks list length " + tl.Tasks.Length);
  7.         }
  8.     }
  9.  
  10.     class Task
  11.     {
  12.         private readonly TasksList list;
  13.         private readonly int id;
  14.  
  15.         public Task(TasksList list, int id)
  16.         {
  17.             this.list = list;
  18.             this.id = id;
  19.         }
  20.     }
  21.  
  22.     class TasksList
  23.     {
  24.         private readonly Task[] tasks;
  25.  
  26.         public TasksList(int[] ids)
  27.         {
  28.             var mutableList = new List<Task>();
  29.             foreach (int id in ids)
  30.             {
  31.                 Task t = new Task(this, id);
  32.                 mutableList.Add(t);
  33.             }
  34.  
  35.             this.tasks = mutableList.ToArray();
  36.         }
  37.  
  38.         public Task[] Tasks { get { return tasks; } }
  39.     }
clone this paste RAW Paste Data