public static void TestNarrowing() { long lhs = 34000; long rhs = long.MaxValue; try { int result = lhs.AddNarrowingChecked(rhs); } catch (OverflowException) { // could not be added } // Our two variables are declared and initialized int sourceValue = 34000; short destinationValue = 0; // Determine if sourceValue will lose information in a cast to a short if (sourceValue <= short.MaxValue && sourceValue >= short.MinValue) { destinationValue = (short)sourceValue; } else { // Inform the application that a loss of information will occur } }
public static void Run() { long lhs = 34000; long rhs = long.MaxValue; try { int result = lhs.AddNarrowingChecked(rhs); Console.WriteLine(result); } catch (OverflowException) { Console.WriteLine("OverflowException"); } }