How To Add An Item To The List If It Doesn't Exist In C#? - Josip Miskovic
Maybe your like
To add an item to the list, if it doesn't exist:
- Use the Contains method to check if the item exists in the list. It returns true if the item exists in the list.
- If Contains is true, use the Add method to add the item to the list.
- Create a generic extension method to make it easy to add an item to the list if it doesn't exist.
Performance
The Contains method does a linear, O(n) search. If you have a lot of items in the list, this can be slow.
To get a faster collection search, consider to:
- Use a hashset or a dictionary,
- BinarySearch method on the List
- Sorted List
In most cases, you won't run into performance problems. If you have a list with over 10,000 items, do some performance testing to find which method performs the best.
IEnumerable
To implement the "add if not exists" method using IEnumerable:
- Use Contains on the IEnumerable to check if the item exists in the list.
- If Contains is false, use Append on the IEnumerable to add the item.
- Use a new variable to store the returned value because Append creates a new collection.
We need to create a new variable because in C#, IEnumerable is an immutable collection. It means that we cannot add, modify, or remove items.
The Append method does not change the elements in the collection, it just makes a copy of the collection with the extra element added.
The benefit of using the IEnumerable interface is that it's more general. The code will work with any type that implements IEnumerable, not just List.
For example, we could use the same code to add an item to a string list:
C#string[] words = { "apple", "banana", "carrot" }; IEnumerable<string> newWords = words.AddIfNotExists("celery") .AddIfNotExists("apple") .AddIfNotExists("banana"); Console.WriteLine(string.Join(", ", newWords)); // apple, banana, carrot, celeryHashSet
Consider using a HashSet to prevent duplicate values in a collection.
In C#, HashSet cannot have duplicate elements. The implementation takes care of it for you.
So, if you keep calling the Add method with an existing item, nothing will happen:
C#HashSet<string> items = new (); items.Add("1"); items.Add("1"); items.Add("1"); items.Add("2"); Console.WriteLine(string.Join(", ", items)); // 1, 2However, unlike List, HashSet doesn't guarantee order of items.
Tag » Add Element Ienumerable C#
-
How Can I Add An Item To A IEnumerable
Collection? -
Enumerable.Append
(IEnumerable ... - Microsoft Docs -
C# – How To Add An Item To A IEnumerable Collection - ITecNote
-
Add Element To Ienumerable C# Code Example - Code Grepper
-
How To Add Item In Ienumerable In C# Code Example - Code Grepper
-
Comment Ajouter Un élément à Une Collection IEnumerable
-
C# - How Can I Add An Item To A IEnumerable
Collection? -
IEnumerable In C# - C# Corner
-
C# | Adding The Elements Of The Specified Collection To The End Of The ...
-
Convertir IEnumerable En Liste En C# | Delft Stack
-
How To Add Items To Each Elements Of List
? - CodeProject -
IEnumerable.Append C# (CSharp) Code Examples - HotExamples
-
What's The Best Way To Add One Item To An IEnumerable
? -
Foreach And IEnumerable - Level Up Coding
-
Add Element To The Beginning Of The List, In C# - Programming Idioms
-
How Do I Add An Element To An IEnumerable? - Ru
-
How Can I Add An Item To A IEnumerable< T> Collection - Anycodings
-
Can You Add To IEnumerable? - De Kooktips - Beginpagina
-
IEnumerableExtensions.Append(T) Method (IEnumerable(T), T)