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 Collections - List generic collection - Quiz 8hard Exception Handling - Why exception handling is needed - Quiz 2easy Inheritance - Why inheritance is needed - Quiz 5medium Inheritance - Base class and derived class - Quiz 2easy Interfaces - Interface as contract mental model - Quiz 6medium Interfaces - Interface as contract mental model - Quiz 14medium LINQ Fundamentals - LINQ with custom objects - Quiz 2easy LINQ Fundamentals - Aggregate functions (Count, Sum, Average) - Quiz 5medium Properties and Encapsulation - Properties vs fields - Quiz 4medium Strings and StringBuilder - Common string methods - Quiz 1easy