ConcurrentBag is a C# collection type that provides thread-safe insertion and removal of elements. The Distinct method is used to retrieve a set of unique elements from the collection.
Example 1: Removing duplicate elements from a ConcurrentBag
ConcurrentBag bag = new ConcurrentBag(); bag.Add(1); bag.Add(2); bag.Add(3); bag.Add(1); bag.Add(2); bag.Add(3); var distinctElements = bag.Distinct(); // returns [1, 2, 3]
Example 2: Using custom equality comparer to find distinct elements
ConcurrentBag bag = new ConcurrentBag(); bag.Add("apple"); bag.Add("orange"); bag.Add("Apple"); var distinctElements = bag.Distinct(StringComparer.OrdinalIgnoreCase); // returns ["apple", "orange"]
This code example uses the ConcurrentBag class from the System.Collections.Concurrent namespace, which is included in the .NET Framework Class Library.
C# (CSharp) ConcurrentBag.Distinct - 49 examples found. These are the top rated real world C# (CSharp) examples of ConcurrentBag.Distinct extracted from open source projects. You can rate examples to help us improve the quality of examples.