One = 0b0001; public const int Two = 0b0010; // grouping of numbers public const int Sixteen = 0b0001_0000; public const uint HexNumber = 0xFFFF_0000; public long Billion = 1_000_000_000_000L; public const double AvogadroConstant = 6.022_140_857_747_474e23;
Range(IEnumerable<int> numbers) { int min = int.MaxValue; int max = int.MinValue; foreach (var n in numbers) { min = (n < min) ? n : min; max = (n > max) ? n : max; } return (max, min); }
int min = int.MaxValue; int max = int.MinValue; foreach (var n in numbers) { min = (n < min) ? n : min; max = (n > max) ? n : max; } return (max, min); } Tuples als Returnwert
{ public double X { get; } public double Y { get; } public Point(double x, double y) { this.X = x; this.Y = y; } public void Deconstruct(out double x, out double y) { x = this.X; y = this.Y; } }
double X { get; } public double Y { get; } public void Deconstruct(out double x, out double y) { x = this.X; y = this.Y; } } // Deconstruction var p = new Point(3.14, 2.71); (double X, double Y) = p; Console.WriteLine($"(x,y): ({X},{Y})");
Constant Patterns if (o is null) return; // constant pattern "null" – Type Patterns if (!(o is int i)) return; // type pattern "int i" – Var Patterns • 2 C# language constructs – is – switch
{ case Circle c: Console.WriteLine($"circle with radius {c.Radius}"); break; case Rectangle s when (s.Length == s.Height): Console.WriteLine($"{s.Length} x {s.Height} square"); break; case Rectangle r: Console.WriteLine($"{r.Length} x {r.Height} rectangle"); break; default: Console.WriteLine("<unknown shape>"); break; case null: throw new ArgumentNullException(nameof(shape)); } }
Fib which cannot be called from the outside. // The local function has access to local variables and parameters of the outer function public int Fibonacci(int x) { if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x)); return Fib(x).current; (int current, int previous) Fib(int i) { if (i == 0) return (1, 0); var (p, pp) = Fib(i - 1); return (p + pp, p); } }