C Sharp (C#) - CollectionsWhat happens if you try to add a duplicate element to a HashSet<int> in C#?AThe duplicate element replaces the existing one.BThe duplicate element is ignored and not added.CAn exception is thrown at runtime.DThe HashSet clears all elements before adding the duplicate.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand HashSet behavior on duplicatesA HashSet stores unique elements and ignores duplicates when adding.Step 2: Check what happens on adding a duplicateAdding a duplicate element does not change the set and no error occurs.Final Answer:The duplicate element is ignored and not added. -> Option BQuick Check:HashSet duplicate add = ignored [OK]Quick Trick: HashSet ignores duplicates silently when adding [OK]Common Mistakes:MISTAKESThinking duplicates replace existing elementsExpecting an exception on duplicatesAssuming HashSet clears before adding duplicates
Master "Collections" in C Sharp (C#)9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More C Sharp (C#) Quizzes Classes and Objects - Static members vs instance members - Quiz 7medium Exception Handling - Exception hierarchy in .NET - Quiz 5medium Inheritance - Sealed classes and methods - Quiz 4medium Interfaces - Why interfaces are needed - Quiz 13medium Interfaces - Interface vs abstract class decision - Quiz 1easy Interfaces - Interface as contract mental model - Quiz 1easy Polymorphism and Abstract Classes - Why polymorphism matters - Quiz 6medium Polymorphism and Abstract Classes - Casting with as and is operators - Quiz 5medium Properties and Encapsulation - Auto-implemented properties - Quiz 6medium Strings and StringBuilder - Verbatim and raw string literals - Quiz 6medium