EMZETT.
Login

Kurz: // Kontrolle if (…) { } else if (…) { } else { } var ergebnis = wert switch { > 10 => “gross”, _ => “klein” }; for (…) { } foreach (var e in liste) { } while (…) { } do { } while (…);

Teil des Kurses C#

Kapitel 16 von 16 im Kurs C# (Abschnitt „Nachschlagen“). Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

Typen

Typ.NET-NameBereich
byteByte0 bis 255
shortInt16±32.767
intInt32±2,1 Mrd.
longInt64±9,2 · 10¹⁸
floatSingleca. 7 Stellen
doubleDoubleca. 15 Stellen
decimalDecimal28 Stellen, exakt (Geld)
boolBooleantrue/false
charCharein Zeichen
stringStringText
objectObjectBasis aller Typen
DateTime, DateOnly, TimeOnly, TimeSpanZeit
Guideindeutige ID

Syntax-Überblick

// Variablen und Konstanten
var x = 5; const int Max = 10; string? n = null;
 
// Kontrolle
if (...) { } else if (...) { } else { }
var ergebnis = wert switch { > 10 => "gross", _ => "klein" };
for (...) { } foreach (var e in liste) { } while (...) { } do { } while (...);
 
// Methoden
int Add(int a, int b) => a + b;
void Tausche(ref int a, ref int b) { }
bool TryX(string s, out int w) { w = 0; return true; }
 
// Klassen
class A : B, IX { public int P { get; set; } public A() { } public override string ToString() => ""; }
record R(string Name, int Alter);
struct S { public int X; }
enum E { Eins, Zwei }
interface IX { void M(); }
 
// Fehler
try { } catch (Exception e) when (...) { } finally { }
throw new ArgumentException("...");
 
// Async
async Task<int> LadeAsync() { await Task.Delay(1); return 1; }
 
// LINQ
liste.Where(x => x > 1).Select(x => x * 2).OrderBy(x => x).ToList();
 
// Null
a ?? b; a?.B; a ??= b; a!;
 
// Muster
if (o is string s && s.Length > 0) { }

Häufige Klassen und Namensräume

Namensraum / KlasseZweck
System: Console, Math, Convert, DateTime, Random, EnvironmentGrundlagen
System.Collections.Generic: List<T>, Dictionary<K,V>, HashSet<T>, Queue<T>, Stack<T>Sammlungen
System.LinqLINQ
System.IO: File, Directory, Path, StreamReaderDateien
System.Text: StringBuilder, EncodingText
System.Text.JsonJSON
System.Text.RegularExpressionsRegex
System.Threading.Tasks: Task, ParallelAufgaben
System.Net.Http: HttpClientHTTP
System.Globalization: CultureInfoKultur
System.Diagnostics: Stopwatch, DebugMessen, Debuggen

Format-Spickzettel

FormatBeispielErgebnis (de-DE)
{x:F2}3.141593,14
{x:N0}12345671.234.567
{x:C}19.919,90 €
{x:P1}0.25625,6 %
{x:D5}4200042
{x:X}255FF
{d:dd.MM.yyyy HH:mm}DateTime08.03.2025 14:30
{s,10} / {s,-10}Breiterechts- / linksbündig

Konventionen

  • Typen, Methoden, Eigenschaften: PascalCase
  • Lokale Variablen, Parameter: camelCase
  • Private Felder: _camelCase
  • Interfaces: IName
  • Async-Methoden enden auf Async
  • Eine Klasse pro Datei, Dateiname = Klassenname

Weiterlernen

  • learn.microsoft.com/dotnet/csharp: offizielle Dokumentation und Tutorials
  • Praxis: ASP.NET Core Web-API, Blazor (Weboberflächen in C#), Unity (Spiele), MAUI (Apps)
  • Bücher: “C# in a Nutshell”, “Pro C#”
  • Üben: Exercism, Advent of Code, kleine Web-API mit SQLite

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Projekte, NuGet und Tests

Alle Kapitel: C# im Überblick