Recap - HashSet
Part of the Logic & Flow section of Coddy's C# journey — lesson 61 of 66.
Challenge
EasyCreate a method named ProcessHashSet that performs multiple operations on a HashSet:
- Create a new HashSet of strings
- Add three fruit names: "Apple", "Banana", and "Orange"
- Print whether "Mango" exists in the set (use the format: "Contains Mango: True/False")
- Try to add "Apple" again and print whether it was added (use the format: "Added Apple again: True/False")
- Remove "Banana" and print whether it was successfully removed (use the format: "Removed Banana: True/False")
- Print the current count of elements (use the format: "Count: N")
- Return the HashSet
Try it yourself
using System;
using System.Collections.Generic;
class Program
{
public static HashSet<string> ProcessHashSet()
{
// Write your code here
return null;
}
static void Main(string[] args)
{
HashSet<string> result = ProcessHashSet();
if (result != null)
{
Console.WriteLine(string.Join(", ", result));
}
}
}All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling8Data Analysis System
Data Collection SetupData Entry Logic11HashSet Part 1
What is a HashSet?Adding an ElementRemoving an ElementChecking if an Element ExistsEmpty and SizeRecap - HashSet3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety