Keywords: C# | List Operations | Union Algorithms
Abstract: This paper explores various methods for merging two lists in C#, focusing on the core mechanisms and application scenarios of AddRange, Union, and Concat. Through detailed code examples and performance comparisons, it explains how to select the most appropriate union operation strategy based on requirements, while discussing the advantages and limitations of LINQ queries in set operations. The article also covers key practical considerations such as list deduplication and memory efficiency.
Basic Concepts of List Union Operations
In C# programming, merging two lists is a common task when handling collection data. According to mathematical set theory, the union of two sets contains all elements that belong to either set, without duplicates. In the .NET framework, the List<T> class provides multiple methods to achieve this, each with specific behaviors and use cases.
AddRange Method: Direct Merging
The AddRange method is an instance method of the List<T> class that directly appends all elements of another collection to the end of the current list. This approach is straightforward but does not automatically remove duplicates. For example:
var listA = new List<int>{1, 2, 3};
var listB = new List<int>{3, 4, 5};
listA.AddRange(listB);
// listA now contains: 1, 2, 3, 3, 4, 5
As shown, the element 3 appears twice because AddRange does not check for duplicates. If deduplication is needed after merging, you can combine it with the Distinct method: listA = listA.Distinct().ToList();. This method is suitable for scenarios where preserving the original order and tolerating temporary duplicates is acceptable.
Union Method: Mathematical Union
Union is a LINQ extension method that returns the union of two sequences, automatically removing duplicates. Unlike AddRange, Union does not modify the original lists but returns a new IEnumerable<T> sequence. For example:
var listA = new List<int>{1, 2, 3};
var listB = new List<int>{3, 4, 5};
var result = listA.Union(listB);
// result contains: 1, 2, 3, 4, 5
The Union method uses the default equality comparer (EqualityComparer<T>.Default) to determine duplicates. For custom types, equality logic can be defined by implementing the IEquatable<T> interface or providing a custom comparer. This method is ideal for scenarios requiring a strict mathematical union without altering the original data.
Concat Method: Sequence Concatenation
The Concat method is another LINQ extension that simply concatenates two sequences without any deduplication. Similar to Union, it returns a new sequence. For example:
var listA = new List<int>{1, 2, 3};
var listB = new List<int>{3, 4, 5};
var result = listA.Concat(listB);
// result contains: 1, 2, 3, 3, 4, 5
Concat behaves similarly to AddRange but is lazily evaluated, meaning elements are processed only when enumerated. This can improve memory efficiency when handling large datasets. However, if immediate materialization is needed, methods like ToList() or ToArray() can be called.
Performance and Memory Considerations
Performance is a key factor when choosing a merging method. AddRange is typically the fastest, as it directly manipulates the internal array with a time complexity of O(n), where n is the size of the second list. However, if deduplication is required later, an additional Distinct call adds overhead. The Union method uses a hash set internally to ensure uniqueness, with an average time complexity of O(n + m), where n and m are the sizes of the two lists, though this may slightly increase due to hash collisions. Concat is lazy, so initial overhead is minimal, but materialization can incur extra costs if all elements need to be accessed immediately.
In terms of memory, AddRange may expand the original list's capacity, potentially causing reallocation. Union and Concat return new sequences, but Union requires additional memory for the hash set. In practice, selection should be based on data scale and real-time requirements.
Practical Application Examples
Suppose we are developing a social network application and need to merge user friend lists. Using Union ensures that the same friend is not added repeatedly:
var currentFriends = new List<string>{"Alice", "Bob", "Charlie"};
var newFriends = new List<string>{"Charlie", "David", "Eve"};
var allFriends = currentFriends.Union(newFriends).ToList();
// allFriends contains: Alice, Bob, Charlie, David, Eve
If we need to log all interaction history, including duplicate events, Concat can be used:
var history = logEntries.Concat(newEntries);
The article also discusses the essential difference between HTML tags like <br> and the character \n, emphasizing the importance of proper escaping in text processing.
Conclusion and Recommendations
When implementing list union operations in C#, AddRange, Union, and Concat each have their strengths and weaknesses. Use AddRange for in-place modifications with tolerance for temporary duplicates; Union for mathematical unions without altering original data; and Concat for simple concatenation with lazy evaluation. Combined with other LINQ methods like Intersect (for intersections) and Except (for differences), more complex set operations can be constructed. In real-world development, it is advisable to choose the most suitable method based on specific needs and data characteristics, and conduct performance testing to optimize code.