Empty and Size
Teil des Abschnitts Logik & Ablauf der C#-Journey von Coddy — Lektion 60 von 66.
HashSets bieten Methoden, um zu prüfen, ob sie leer sind, und um ihre Größe zu ermitteln.
Eine leere HashSet erstellen
HashSet<string> colors = new HashSet<string>();Prüfen, ob das HashSet leer ist, mit Count
bool isEmpty = colors.Count == 0;
// isEmpty ist trueFügen Sie einige Elemente zur HashSet hinzu
colors.Add("Red");
colors.Add("Blue");
colors.Add("Green");Die Größe des HashSet abrufen
int size = colors.Count;
// size is 3Sie können auch mit der Any()-Methode prüfen, ob ein HashSet leer ist.
bool hasElements = colors.Any();
// hasElements is true since the set contains elementsAufgabe
EinfachErstellen Sie eine Methode namens CountAndCheck, die einen HashSet von Strings als Argument nimmt. Die Methode sollte:
- Überprüfen, ob der HashSet leer ist.
- "Empty set" ausgeben, wenn er leer ist.
- Andernfalls "Set contains {count} elements" ausgeben, wobei {count} die Anzahl der Elemente im HashSet ist.
Spickzettel
Prüfen, ob ein HashSet leer ist, mit Count:
bool isEmpty = colors.Count == 0;Die Größe eines HashSets ermitteln:
int size = colors.Count;Prüfen, ob ein HashSet Elemente enthält, mit Any():
bool hasElements = colors.Any();Probier es selbst
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void CountAndCheck(HashSet<string> set)
{
// Schreibe deinen Code hier
}
static void Main(string[] args)
{
// Lese Eingabe für HashSet-Elemente, getrennt durch Kommas
// Wenn die Eingabe leer ist, erstelle einen leeren HashSet
string input = Console.ReadLine();
HashSet<string> set = new HashSet<string>();
if (!string.IsNullOrEmpty(input))
{
string[] elements = input.Split(',');
foreach (string element in elements)
{
set.Add(element.Trim());
}
}
CountAndCheck(set);
}
}Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.
Alle Lektionen in Logik & Ablauf
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