Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Static classes, enums with Flags, and records // Static utility class public static class StringExtensions { public static string Truncate(this string s, int maxLength) => s.Length <= maxLength ? s : s[..maxLength] + "..."; public static bool IsPalindrome(this string s) { var clean = s.ToLower().Where(char.IsLetterOrDigit).ToArray(); return clean.SequenceEqual(clean.Reverse()); } } Console.WriteLine("Hello, World!".Truncate(5)); // Hello... Console.WriteLine("racecar".IsPalindrome()); // True // Flags enum — combine with | [Flags] public enum Permissions { None = 0, Read = 1, Write = 2, Delete = 4 } var userPerms = Permissions.Read | Permissions.Write; Console.WriteLine(userPerms); // Read, Write Console.WriteLine(userPerms.HasFlag(Permissions.Delete)); // False // Record (reference type, value equality) public record OrderLine(string Sku, int Quantity, decimal UnitPrice) { public decimal Total => Quantity * UnitPrice; } var line1 = new OrderLine("ABC-001", 2, 9.99m); var line2 = new OrderLine("ABC-001", 2, 9.99m); Console.WriteLine(line1 == line2); // True (value equality) Console.WriteLine(line1.Total); // 19.98 // Non-destructive mutation with 'with' var line3 = line1 with { Quantity = 5 }; Console.WriteLine(line3); // OrderLine { Sku = ABC-001, Quantity = 5, UnitPrice = 9.99 } // Deconstruct record var (sku, qty, price) = line1; Console.WriteLine($"{sku}: {qty} @ ${price}");
Result
Open