fork download
  1. using System;
  2.  
  3. public static class NumericParsingExtender
  4. {
  5. public static Int32 ToInt32(this String input, Int32 defaultValue = 0)
  6. {
  7. if (String.IsNullOrEmpty(input)) return defaultValue;
  8. Int32 val;
  9. return Int32.TryParse(input, out val) ? val : defaultValue;
  10. }
  11. }
  12.  
  13. public class PseudoControl
  14. {
  15. public String Text { get; set; }
  16. public PseudoControl(String text)
  17. {
  18. this.Text = text;
  19. }
  20. }
  21.  
  22. public class Test
  23. {
  24. public static void Main()
  25. {
  26. PseudoControl txtChild1 = new PseudoControl("12");
  27. PseudoControl txtChild2 = new PseudoControl(null);
  28. PseudoControl txtChild3 = new PseudoControl(String.Empty);
  29. PseudoControl txtWife1 = new PseudoControl("42");
  30. PseudoControl txtWife2 = new PseudoControl("invalid");
  31. PseudoControl txtWife3 = new PseudoControl("20.13");
  32.  
  33. Int32 total = txtChild1.Text.ToInt32()
  34. + txtChild2.Text.ToInt32()
  35. + txtChild3.Text.ToInt32()
  36. + txtWife1.Text.ToInt32()
  37. + txtWife2.Text.ToInt32()
  38. + txtWife3.Text.ToInt32();
  39. Console.WriteLine("Total: {0}", total);
  40. }
  41. }
Success #stdin #stdout 0.04s 33928KB
stdin
Standard input is empty
stdout
Total: 54