



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




C# is a powerful Object Orientated language, for those coming from Java or C++ you should be able to pick up the syntax for C# quickly. A few points: The language is case-sensitive (So A and a are different) Lines terminate with semi-colons Code is put in code blocks { } Inline comments start with // Block comments start with /* */ XML comments start with ///
To declare a variable you specify the data type and variable name followed by a value.
DataType variableName = value;
Variables must start with underscore or letter Variables cannot contain spaces variables can contain numbers Cannot contain symbols (accept underscore)
string Name = "thecodingguys"; int Year = 201 3; I will use these two variables throughout.
Arrays are similar to variables, but can hold more than one value.
DataType[ ] ArrayName = { Comma Separated Values } // Array of any size DataType[] ArrayName = new DataType[ 3 ] {Command Separated Values } //Expects 3 values
string[] MyGamesOf2013 = {"GTAV", "Battlefield3"}; string[] MyMoveisOf2013 = new string[3] {"The Amazing Spiderman", "The Expendables 2", "Rise of the planet of the apes"};
Record structures allow you to store multiple data types under one identifier name. You can create an array of them to store lots of data
Must be declared outside the of any method as a global public struct StructName { public string field1; public int field2; public string field3; public int field4; } Run within a method: StructName [] ArrayName = new StructName [20];
public struct Results { public string hometeam; public int hometeamscore; public string awayteam; public int awayteamscore; } Run within a method: Results[] results = new Results[20];
Similar to the If else statement, however it has these benefits. Much easier to read and maintain Much cleaner then using nested if else It only evaluates one variable
switch (switch_on) { default: }
switch (Year) { case 2013 : Console.WriteLine("It's 20 1 3!"); break; case 2012 : Console.WriteLine("It's 20 1 2!"); break; default: Console.WriteLine("It's " + Year + "!"); break; } The break keyword is required as it prevents case falling.
Continuously loops code until the condition becomes false.
while (true) { }
while (Year >= 2013) { if (Year != 2 100 ) { Console.WriteLine(Year++); } else { } } break; Make sure your condition evaluates to false at some point otherwise the loop is endless and it can result in errors.
Similar to the While Loop, but you specify when the loop will end.
for (int i = 0 ; i < length; i++) { }
for (int i = 0 ; i <= 1 00 ; i++) { Console.WriteLine(i); } This prints out 1 to 100. The expression can be easily broken down like this: I = 0 ; I Is less than or equal to 100? (True) Increment I by 1 When I reaches 100 it will stop because I will no longer be less than 100 and will equal 100 so the condition is false.
The for each loop is used to loop around a collection. (Such as an array)
foreach (var item in collection) { }
foreach (string movie in MyMoveisOf 201 3) { Console.WriteLine(movie); } Outputs all the elements in the MyMoviesOf 2013 array.
To catch any exceptions which are likely to occur you use a try catch block.
try { } catch (Exception) { }
try { string result = "k"; Console.WriteLine(Convert.ToInt 32 (result) + 10 ); } catch (Exception ex) { Console.WriteLine(ex.Message); } The above code results in a format exception, because you can’t convert K to a number