80 likes | 244 Views
C# Basics. Variables, Loops, Decision Statements, etc. byte - 0 to 255 char - 2 bytes bool sbyte - -128 to 127 short - 2 byte int ushort - 0 to 65,535 int - 4 bytes uint - 4 bytes positive. float double decimal long ulong string. Variables Declarations. Using Variables.
E N D
C# Basics Variables, Loops, Decision Statements, etc
byte - 0 to 255 char - 2 bytes bool sbyte - -128 to 127 short - 2 byte int ushort - 0 to 65,535 int - 4 bytes uint - 4 bytes positive float double decimal long ulong string Variables Declarations
Using Variables • C# is Strongly Typed float x = 10.9f; double y = 15.3; y = x; // okay x = (float) y; // conversion required • Variables must have Value before being used. • Hence declarations usually include initialization
Simple Arrays int[] myArray1 = new int[5]; int[] myArray2 = {1, 2, 3, 4, 5}; for (int i=0;i<10; i++) Console.Write ("{0} ",myarray[i]); Console.WriteLine();
Looping Statements • Same as C++ • while (count > 0) process_list (count--); • do process_list( ++count ); while (count < 10); • for (int i=1; i<=10; i++) • Different from C++ • while (count)// illegal C# unless count is bool
if (count >= 10) { dostuff(); domorestuff(); } else ... switch (choice) { case 'Y': //must be empty case 'y': do_yes_stuff(); break; default: ... Decision Statements Almost Just Like C++
Simple Console Output System.Console.WriteLine System.Console.Write System.Consolue.WriteLine ("count = {0} and sum = {1}", count, sum);
Simple Console Input string inputline; char charvalue; int intvalue; Console.Write ("Enter a string: "); inputline = Console.ReadLine(); Console.WriteLine("You just entered \"{0}\"",inputline); Console.Write ("Enter a character: "); charvalue = (char) Console.Read(); Console.WriteLine("You just entered \"{0}\"", charvalue); Console.ReadLine(); Console.Write ("Enter an integer: "); inputline = Console.ReadLine(); intvalue = Convert.ToInt32(inputline); Console.WriteLine("You just entered \"{0}\"", intvalue);