Empty and Size
Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 60 / 66.
HashSet'ler boş olup olmadıklarını kontrol etmek ve boyutlarını belirlemek için yöntemler sağlar.
Boş bir HashSet oluşturun
HashSet<string> colors = new HashSet<string>();Count kullanarak HashSet'in boş olup olmadığını kontrol edin
bool isEmpty = colors.Count == 0;
// isEmpty is trueHashSet'e bazı öğeler ekleyin
colors.Add("Red");
colors.Add("Blue");
colors.Add("Green");HashSet'in boyutunu alın
int size = colors.Count;
// size is 3Ayrıca bir HashSet'in boş olup olmadığını Any() metodunu kullanarak kontrol edebilirsiniz.
bool hasElements = colors.Any();
// hasElements true çünkü küme elemanlar içeriyorGörev
KolayCountAndCheck adında bir yöntem oluşturun ki, string türünden bir HashSet'i argüman olarak alsın. Yöntem şunları yapmalı:
- HashSet'in boş olup olmadığını kontrol edin.
- Boşsa "Empty set" yazdırın.
- Aksi takdirde, HashSet'teki eleman sayısını belirten "Set contains {count} elements" yazdırın; burada {count} HashSet'teki eleman sayısıdır.
Kopya kağıdı
Bir HashSet'in boş olup olmadığını Count kullanarak kontrol edin:
bool isEmpty = colors.Count == 0;Bir HashSet'in boyutunu alın:
int size = colors.Count;Bir HashSet'in elemanları olup olmadığını Any() kullanarak kontrol edin:
bool hasElements = colors.Any();Kendin dene
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static void CountAndCheck(HashSet<string> set)
{
// Kodu buraya yazın
}
static void Main(string[] args)
{
// Virgülle ayrılmış HashSet elemanları için girişi oku
// Giriş boşsa, boş bir HashSet oluştur
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);
}
}Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.
Mantık & Akış bölümündeki tüm dersler
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