Typen
| Typ | .NET-Name | Bereich |
|---|---|---|
byte | Byte | 0 bis 255 |
short | Int16 | ±32.767 |
int | Int32 | ±2,1 Mrd. |
long | Int64 | ±9,2 · 10¹⁸ |
float | Single | ca. 7 Stellen |
double | Double | ca. 15 Stellen |
decimal | Decimal | 28 Stellen, exakt (Geld) |
bool | Boolean | true/false |
char | Char | ein Zeichen |
string | String | Text |
object | Object | Basis aller Typen |
DateTime, DateOnly, TimeOnly, TimeSpan | Zeit | |
Guid | eindeutige 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 / Klasse | Zweck |
|---|---|
System: Console, Math, Convert, DateTime, Random, Environment | Grundlagen |
System.Collections.Generic: List<T>, Dictionary<K,V>, HashSet<T>, Queue<T>, Stack<T> | Sammlungen |
System.Linq | LINQ |
System.IO: File, Directory, Path, StreamReader | Dateien |
System.Text: StringBuilder, Encoding | Text |
System.Text.Json | JSON |
System.Text.RegularExpressions | Regex |
System.Threading.Tasks: Task, Parallel | Aufgaben |
System.Net.Http: HttpClient | HTTP |
System.Globalization: CultureInfo | Kultur |
System.Diagnostics: Stopwatch, Debug | Messen, Debuggen |
Format-Spickzettel
| Format | Beispiel | Ergebnis (de-DE) |
|---|---|---|
{x:F2} | 3.14159 | 3,14 |
{x:N0} | 1234567 | 1.234.567 |
{x:C} | 19.9 | 19,90 € |
{x:P1} | 0.256 | 25,6 % |
{x:D5} | 42 | 00042 |
{x:X} | 255 | FF |
{d:dd.MM.yyyy HH:mm} | DateTime | 08.03.2025 14:30 |
{s,10} / {s,-10} | Breite | rechts- / 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