-
Notifications
You must be signed in to change notification settings - Fork 0
Conversion Methods
Ahmad Al-freihat edited this page Jan 1, 2026
·
1 revision
NonEmptyList<T> provides methods to convert to various .NET collection types.
Convert to a standard array:
NonEmptyList<int> list = new(1, 2, 3);
int[] array = list.ToArrayNonEmpty();
// Result: [1, 2, 3]Convert to a HashSet (removes duplicates):
NonEmptyList<int> list = new(1, 2, 2, 3, 3);
HashSet<int> set = list.ToHashSet();
// Result: {1, 2, 3}Convert to a Dictionary using a key selector:
NonEmptyList<string> words = new("apple", "banana", "cherry");
Dictionary<char, string> dict = words.ToDictionary(w => w[0]);
// Result: {'a': "apple", 'b': "banana", 'c': "cherry"}With both key and value selectors:
NonEmptyList<string> words = new("apple", "banana");
Dictionary<char, int> dict = words.ToDictionary(
w => w[0], // Key selector
w => w.Length // Value selector
);
// Result: {'a': 5, 'b': 6}Convert to a Queue (FIFO):
NonEmptyList<int> list = new(1, 2, 3);
Queue<int> queue = list.ToQueue();
int first = queue.Dequeue(); // 1
int second = queue.Dequeue(); // 2Convert to a Stack (LIFO):
NonEmptyList<int> list = new(1, 2, 3);
Stack<int> stack = list.ToStack();
int top = stack.Pop(); // 3
int next = stack.Pop(); // 2Convert to a LinkedList:
NonEmptyList<int> list = new(1, 2, 3);
LinkedList<int> linked = list.ToLinkedList();Get a read-only view:
NonEmptyList<int> list = new(1, 2, 3);
IReadOnlyList<int> readOnly = list.AsReadOnly();Convert to an immutable variant:
NonEmptyList<int> mutable = new(1, 2, 3);
ImmutableNonEmptyList<int> immutable = mutable.ToImmutable();- Learn about Utility Methods for additional operations
- Explore ImmutableNonEmptyList for the immutable variant