Last active
December 24, 2015 08:39
-
-
Save rofr/6772234 to your computer and use it in GitHub Desktop.
Some immutability stuff for informatorbloggen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImmutableList<T> : IEnumerable<T> | |
{ | |
List<T> _items; | |
public ImmutableList() | |
{ | |
_items = new List<T>(); | |
} | |
private ImmutableList(List<T> items) | |
{ | |
_items = items; | |
} | |
public ImmutableList<T> Add(T item) | |
{ | |
var newItems = _items.ToList(); | |
newItems.Add(item); | |
return new ImmutableList<T>(newItems); | |
} | |
public int Count{get{return _items.Count;}} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
foreach(T item in _items) yield return item; | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment