// Compile this program with the following command line: // C:>csc /checked OvrFlow2.cs using System; public class OvrFlow2 { static public void Main () { int large = 2147483647; int larger = large; unchecked { ++larger; larger *= 2; } Console.WriteLine ("large = " + large); Console.WriteLine ("larger = " + larger); } }
Month: February 2011
Operators and Expressions:Checked and Unchecked Expressions
using System; public class CheckedandUncheckedExpressions { public static void Main() { unchecked { byte a = 55; byte b = 210; byte c = (byte) (a + b); } } }
unchecked int overflow
using System; class IntegerOverFlowConst { static void Main() { const ushort MAXUSHORT = 65535; const ushort ONE = 1; unchecked { ushort total = (short)MAXUSHORT + ONE; } } }
Checking for overflows.
using System; public class MainClass { public int CheckedAddition(short s1, short s2) { int z = 0; try { z = checked((short)(s1 + s2)); } catch (OverflowException) { Console.WriteLine("Overflow Exception, Returning 0"); } return z; } public int UncheckedAddition(short s1, short s2) { int z = ((short)(s1 + s2)); return z; } public static void Main() { MainClass app = new MainClass(); short s1 = 32767; short s2 = 32767; Console.WriteLine("Checked Addition is: {0}",app.CheckedAddition(s1, s2)); Console.WriteLine("Unchecked Addition is: {0}",app.UncheckedAddition(s1, s2)); } }
Checked and Unchecked
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Max value of byte is {0}.", byte.MaxValue); Console.WriteLine("Min value of byte is {0}.", byte.MinValue); byte b1 = 100; byte b2 = 250; try { checked { byte sum = (byte)(b1 + b2); byte b4, b5 = 100, b6 = 200; b4 = (byte)(b5 + b6); Console.WriteLine("sum = {0}", sum); } } catch (OverflowException e) { Console.WriteLine(e.Message); } unchecked { byte sum = (byte)(b1 + b2); Console.WriteLine("sum = {0}", sum); } } }
OverflowCheck
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { byte destinationVar; short sourceVar = 281; destinationVar = checked((byte)sourceVar); Console.WriteLine("sourceVar val: {0}", sourceVar); Console.WriteLine("destinationVar val: {0}", destinationVar); } }
Is vowel char
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AroLibraries.ExtensionMethods.Strings { public static class CharExt { private static readonly char[] _Vowel=new char[]{'a','e','u','i','o'}; public static bool Ext_IsVowel(this Char iChar) { if (_Vowel.Contains(Char.ToLower(iChar))) { return true; } return false; } } }