1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 //Enum Definition 12 enum orientation : byte 13 { 14 north=1, 15 south=2, 16 east=3, 17 west=4 18 } 19 20 //Structure Definition 21 struct route 22 { 23 public orientation direction; 24 public double distance; 25 } 26 27 static void Main(string[] args) 28 { 29 #region regionTest 30 Console.WriteLine("From the beginning again."); 31 Console.ReadLine(); 32 #endregion 33 34 //Type Conversion 35 /* Keyword for OverflowException check: checked, unchecked 36 * or you can configure the application: 37 * 1) Right click the project in Solution Explorer panel,select ‘Properties‘ from context menu 38 * 2) Select ‘Build‘ in the left navigation bar, click ‘Advanced‘ button. 39 * 3) Tick off ‘Check for arithmetic overflow/underflow‘ checkbox, click ‘OK‘. 40 */ 41 byte destinationVar; 42 short sourcevar = 281; 43 destinationVar = checked((byte)sourcevar); //will popup an error when running 44 Console.WriteLine("destinationVar val:{0}", destinationVar); 45 46 //Enum practice 47 byte directionByte; 48 string directionString; 49 orientation myDirection = orientation.north; 50 Console.WriteLine("myDirection = {0}",myDirection); 51 directionByte = (byte)myDirection; //must use explicit conversion even though it‘s of byte. 52 53 //enum to string 54 directionString = Convert.ToString(myDirection); 55 directionString = myDirection.ToString(); // same rsult as the one above 56 Console.WriteLine("byte equivalent = {0}",directionByte); 57 Console.WriteLine("string equivalent={0}",directionString); 58 59 //string to enum 60 string myString = "east"; 61 orientation yourDirection = (orientation)Enum.Parse(typeof(orientation), myString); 62 63 ////Struct practice 64 route myRoute; 65 int intDirection = -1; 66 double doubleDistence; 67 Console.WriteLine("1) North\t2) South\t3) East\t4) West"); 68 do 69 { 70 Console.WriteLine("Select a direction:"); 71 intDirection = Convert.ToInt32(Console.ReadLine()); 72 } while (intDirection < 1 || intDirection > 4); 73 Console.WriteLine("Input a distance:"); 74 doubleDistence = Convert.ToDouble(Console.ReadLine()); 75 myRoute.direction = (orientation)intDirection; 76 myRoute.distance = doubleDistence; 77 Console.WriteLine("myRoute specifies a direction of {0} and a distance of {1}.", myRoute.direction, myRoute.distance); 78 79 //Array Definition 80 int[] firstArrary = {1,4,43,68,18}; 81 int[] secondArray=new int[5]; 82 int[] thirdArrary = new int[3] { 1,4,6}; //the number should be the same. 83 const int count = 2; 84 int[] forthArray = new int[count]; 85 86 //multidimensional array 87 double[,] multiArray1 = new double[3, 4]; 88 double[,] multiArray2 = { {1,6},{6,90},{9,2}}; 89 90 //jagged array 91 int[][] jaggedArray = new int[2][]; 92 jaggedArray[0]=new int[4]; 93 jaggedArray[1] = new int[2]; 94 95 int[][] jaggedArray2 = { new int[]{2,7}, new int[]{6}, new int[]{2,10}}; 96 foreach (int[] divisorsOfInt in jaggedArray2) 97 { 98 foreach (int divisor in divisorsOfInt) 99 { 100 Console.WriteLine(divisor); 101 } 102 } 103 104 //String Manipulation 105 string mystring = "Happy New Year"; 106 char[] mychars = mystring.ToCharArray(); 107 foreach (char character in mystring) 108 { 109 Console.WriteLine("{0}", character); 110 } 111 112 Console.ReadLine(); 113 114 } 115 } 116 }