C# Syntax int parrotAge; string parrotStatus = "Alive"; C# requires you to declare the variable type at the same time as the variable name. However it does not require you to assign a value to the variable straight away. int parrotAge; string parrotStatus = "Alive"; parrotAge = 12; System.Console.WriteLine("The parrot is currently " + parrotAge + " and is " + parrotStatus); Once assigned you can use the variable with other values or variables as shown. Note that if you leave the variable ‘null’ when you declare it, you must assign a value to the variable first before being able to carry out further operations on it. parrotAge = parrotAge + 1; Console.WriteLine(parrotAge); A variable can be overwritten with a new value at any time. You cannot assign data to a variable of different types. Each variable will only hold the data type defined. As you can see – an error is shown in the IDE as “two” is a string, and we are trying to assign it to an ‘int’. Keywords for C# data types Data Type Range byte 0 .. 255 sbyte -128 .. 127 short -32,768 .. 32,767 ushort 0 .. 65,535 int -2,147,483,648 .. 2,147,483,647 uint 0 .. 4,294,967,295 long -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
5 It should be noted that that value of x changes in sequence, line by line as it is interpreted, at the start of line 3 (x=x+y) x still has a value of 3 but once that line is run it then changes to be x+y or 5. C# int x, y; x = 3; y = 2; x = x + y; Console.WriteLine(x); Selection Pseudocode if entry==“a” then print(“You selected A”) elseif entry==“b” then print(“You selected B”) else print(“Unrecognised selection”) endif Selection will be carried out with if/else and switch/case. In this example, the pseudocode is checking the input and returning a message based upon the specific input given, the else block is used as a catch for any unexpected input which allows the code to degrade gracefully. The switch/case method works in the same way. switch entry: case “A”: print(“You selected A”) case “B”: print(“You selected B”) default: print(“Unrecognised selection”) endswitch The CASE SELECT is slightly different in that you always have to have the ‘default’ case. This effectively creates a ‘Catch’ for when invalid entry is recognized. In an IF statement you would have to specifically create an ELSE construct to cope for this. IF or CASE Select? IF statements use relational operators to make decisions. Is a variable equal to that variable, is it greater than etc. Case Selects only look for exact matches, via the ‘case’ line. Therefore you have to make a decision as to what choice you are making, as to which may be most appropriate. C#
work! The first way is perhaps viewed as more “formal” as it clearly shows the type of array being declared again in the assignment. Assigning data items to an array after initialisation int[] peopleAges = new int[5]; peopleAges[0] = 31; peopleAges[1] = 24; peopleAges[2] = 12; peopleAges[3] = 68; peopleAges[4] = 44; After we have initialised the array, we can add data items, of that data type, into the array. All of the data items within an array must be of the same type. Here we have added 5 ages to our array. Remember that an array index starts at [0]! string[,] namesAndAges = new string[3, 2]; namesAndAges[0, 0] = "Jill"; namesAndAges[0, 1] = "23"; namesAndAges[1, 0] = "Rashpal"; namesAndAges[1, 1] = "43"; namesAndAges[2, 0] = "Leoung"; namesAndAges[2, 1] = "55"; With a 2D array, we need to make sure that: All the data items are of the same type – therefore ages here are stored as strings We remember the dimensions of the array when assigning data! Trying to assign a variable outside of the array boundary string[,] namesAndAges = new string[3, 2]; namesAndAges[0, 0] = "Jill"; namesAndAges[0, 3] = "23"; The code on the left shows us that we have tried to assign ‘23’ to [0,3] in the array. The array’s bounds are [3,2] and therefore [0,3] does not exist. It generates the following run time error shown below. These errors do not show in the IDE, and will simply result in the program crashing once executed as shown below. Array out of bounds run time error: Sorting arrays Sorting lists is usually useful and you can do this by using the Array.Sort() method. However, once sorted, the array will remain in a sorted state
Notice that each variable has extra key words before its declaration: public const public static The keyword public shows us this is a globally accessible variable. When trying to access the variables, note we now have to use the MyGlobals. before the name of the variable we want to use. The use of SQL to search for data Pseudocode SELECT (including nested SELECTs) FROM (including use of * wildcard) WHERE LIKE (with % used as a wildcard) AND OR SQL This example assumes there is a database created called ‘Customers’ with columns called: CustomerID CustomerName ContactName Address City Country. SELECT * FROM Customers This selects everything (*) from the Customers database. SELECT ContactName,Address FROM Customers WHERE ContactName = ‘Mr Jones’; This selects the ContactName and Address columns from the Customers table and then specifically looks for a “Mr Jones” in the ContactName field. SELECT ContactName,Address FROM Customers WHERE CustomerID = 3; This selects the ContactName and Address columns from the Customers table where the CustomerID field equals 3.