// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; public class Guard11 { public static void M1(string? v) { ArgumentNullException.ThrowIfNull(v, "v"); } public static void M2(int v) { ArgumentOutOfRangeException.ThrowIfEqual(v, 0); ArgumentOutOfRangeException.ThrowIfNegative(v); } public static void Test1(string? v) { try { M1(v); Console.WriteLine("OK"); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static void Test2(int v) { try { M2(v); Console.WriteLine("OK"); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static void Main(string[] args) { Test1("ABC"); Test1(null); Test2(1); Test2(0); Test2(-1); } }