Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Method overloading, expression-bodied members, and local functions static class MathUtils { // Method overloading — same name, different signatures public static int Add(int a, int b) => a + b; public static double Add(double a, double b) => a + b; public static int Add(int a, int b, int c) => a + b + c; // Full-body method public static double HypotenuseLength(double a, double b) { double sumSquares = (a * a) + (b * b); return Math.Sqrt(sumSquares); } // Generic method public static T Clamp<T>(T value, T min, T max) where T : IComparable<T> { if (value.CompareTo(min) < 0) return min; if (value.CompareTo(max) > 0) return max; return value; } } Console.WriteLine(MathUtils.Add(1, 2)); // 3 Console.WriteLine(MathUtils.Add(1.5, 2.5)); // 4.0 Console.WriteLine(MathUtils.Add(1, 2, 3)); // 6 Console.WriteLine(MathUtils.HypotenuseLength(3, 4)); // 5 // Local function example static string FormatName(string first, string? middle, string last) { return $"{Normalise(first)} {(middle is null ? "" : Normalise(middle) + " ")}{Normalise(last)}".Trim(); // Local function — only visible inside FormatName static string Normalise(string s) => string.IsNullOrWhiteSpace(s) ? "" : char.ToUpper(s[0]) + s[1..].ToLower(); } Console.WriteLine(FormatName("alice", null, "SMITH")); // Alice Smith Console.WriteLine(FormatName("bob", "james", "jones")); // Bob James Jones
Result
Open