// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Program { public static void TestConvert(object o) { try { Console.WriteLine(" Convert: {0} -> {1}", o, Convert.ToInt32(o)); } catch { Console.WriteLine(" Convert: {0} is not a number", o); } } public static void TestParse(object o) { try { Console.WriteLine(" Parse: {0} -> {1}", o, int.Parse(o.ToString())); } catch { Console.WriteLine(" Parse: {0} is not a number", o); } } public static void Test(object o) { Console.WriteLine("{0}:", o.GetType().Name); TestConvert(o); TestParse(o); Console.WriteLine("{0} as string:", o.GetType().Name); TestConvert(o.ToString()); TestParse(o.ToString()); } public static void Main(string[] args) { Test("ABC"); Test(123); Test(123.45); Test(false); } } }