Keywords are directives built into the programming language which have special meaning to the compiler Using: imports source code that someone else made which we want to access
Namespace, class and method (in this case, 'Main') group code together in a hierarchy of code blocks. The 'Program' class below is said to be a member of the 'HelloWorld' namespace. The 'Main' method is a member of the 'Program' class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld { // The highest level grouping of code. Larger programs may organise their code in more namespaces with more classes and methods in them
class Program {
static void Main(string[] args) { // This particular setup for a method indicates that this is the starting point for the program
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
Convert Methods:
Converting string user input to an integer:
string age = Console.ReadLine(); int playerAge = Convert.ToInt32(age)
Typecasting:
Implicit typecasting: wider conversion that happens without command. Eg. when int vs. long, int is converted to long
Explicit typecasting: to force the conversion of wider type to narrower.
long a = 3; int b = (int)a;
***(int) is treated as a typical operator in math operations
Special real numbers:
Infinity: float.PositiveInfinity
NaN (not a number): float.NaN
e and π: Math.PI and *Math.E **Overflow: when the max range of a data type is exceeded
Relational operators: ==, !=, <=, >=, <, > To define the condition's conditions
Negate condition: if (!condition) checks if the condition is false to return true
Conditional operators: (works exactly the same as in Python) and: && or: || Extra: ternary operator (works on 3 values): ? General form: (condition) ? value if true : value if false eg: (ATAR > 99) ? "You win" : "You failed!"
Switch statements: An alternative to if/elif/elif/elif... chains
int menuChoice;
...
switch (menuChoice) { // The variable that can take different values (which would lead to different 'cases') is written in parentheses
case 1:
code;
break;
case 2:
code;
break;
default: // Basically means 'else'. This is where the program execution defaults to when no cases are matched
break;
}
While loop: while (condition) { } — this checks before executing do { } while (condition); — this executes at least once before checking while and deciding whether or not to execute further iterations. Runs the code at least once whereas a normal while loop will check before running first
For loop: for (init; condition; increment) Init — declare and initialise loop control variables (executed first and only once.) Condition — checked and evaluated to either true or false Increment — the action at the end of the loop, usually increments the looping variable by 1
for (int x = 1; x <= 10; x++) { // In the initial condition, a variable is both declared and intialised
Console.WriteLine(x); // Writes to the console 1-10. When x=11, the second argument in the for loop is false and so the loop is suspended.
}
Foreach loop: "Do this particular task for each element in the array"
int[] scores = new int[10];
foreach (int score in scores)
{
Console.WriteLine("Someone had a score of: " + score); // You have no way of knowing what idnex you are currently at, but in many cases this isn't a big deal. Use regular for loops if this matters
}
Foreach loops tend to be more readable
Arrays: stores data of the same type as several array elements in an ordered indexed sequence.
int[] scores; // Declaring the variable
scores = new int[10]; // Setting the maximum index. This cannot be changed later on
scores[0] = 10; // Assigning a value to the first array element
Matrices: 'arrays of arrays' or multi-dimensional arrays.
Jagged array: when the smaller arrays have different lengths
int[][] matrix; // Declaring the larger array
matrix = new int[4][]; // Setting the amount of smaller arrays as 4, leaving the length of these smaller arrays undetermined
matrix[0] = new int[3]; // Setting the first small array to have a maximum index of 3
matrix[1] = new int[5];
matrix[2] = new int[9];
matrix[3] = new int[2];
matrix[2][1] = 7; // Assigning the second entry of the third array the value of 7
// To print out the jagged array:
for (int row = 0; row < matrix.Length; row++)
{
for (int col = 0; col < matrix[row].Length; col++)
{
Console.Write(matrix[row][col]);
}
Console.WriteLine();
}
Rectangular Array: when the smaller arrays have the same lengths
int[,] matrix; // Uses [,] instead of [][] like in jagged arrays
matrix = new int[4, 4]; // The second argument is given. This makes matrix a multi-dimensional array with 4 arrays, each with a max range of 4 elements (0-3 index)
matrix[2, 1] = 7;
// To print the rectangular array:
for (int row = 0; row < matrix.GetLength(0); row++) // Uses .GetLength(0) instead of .Length (0 gets the first dimension's length)
{
for (int col = 0; col < matrix.GetLength(1); col++) // Uses .GetLength(1) to get the second dimension's length
{
Console.Write(matrix[row, col]);
}
Console.WriteLine();
}