Advertisement
Guest User

Untitled

a guest
Jun 21st, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.41 KB | None | 0 0
  1. List<MyClass> list = new List<MyClass>();
  2.  
  3. void Main()
  4. {
  5.     for (int i = 0; i < 8; i++)
  6.     {
  7.         Add();
  8.     }
  9.     list.Dump();
  10. }
  11.  
  12. void Add()
  13. {
  14.     if (list.Count == 0)
  15.     {
  16.         list.Add(new MyClass(1));
  17.         return;
  18.     }
  19.     var last = list.Last().Value;
  20.     list.Add(new MyClass(last < 4 ? last + 1 : -last + 1));
  21. }
  22.  
  23. class MyClass
  24. {
  25.     public int Value {get; private set;}
  26.     public MyClass(int value)
  27.     {
  28.         Value = value;
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement