Professional Documents
Culture Documents
C# Cheat Sheet PDF
C# Cheat Sheet PDF
Basic Variables 2
Operators in C# 3
Type Conversion in C# 5
Comparing Stuff in C# 6
Loops in C# 7
Arrays in C# 8
Methods in C# 9
Classes in C# 10
Inheritance in C# 12
Exceptions in C# 13
1
Basic Variables
Variables are the smallest practical working blocks of any application. They are essentially small
pieces of data stored in special purpose containers.
Storing a number:
float myNum = 10.876587f; (‘float’ stores a decimal number - note the ‘f’ ending)
string myText = “I like this sheet”; (‘string’ stores characters or sequences of them)
Variables are the basic building blocks of all software. They cannot be mixed so you cannot store
an int inside of a string or vice versa.
2
Operators in C#
Operators allow sums between numbers to take place. They also allow concatenation of strings
(adding words onto other words).
Adding a number:
int myNum = 9;
Subtracting a number:
int myNum = 9;
Multiplying a number:
int myNum = 9;
Dividing a number:
int myNum = 9;
(The result would be equal to 0). Wait what? Well, ‘int’ can only hold a whole number and 9 / 10
= 0.9. Therefore ‘int result’ simply drops the ‘.9’ part! Be careful when performing arithmetic!
3
Adding strings
4
Type Conversion in C#
You can’t mix types in C# but you can convert between them (with some limitations);
Why an error? You cant convert a word to a number so your app will kick and scream, bringing
the whole thing down. Avoid type conversion where possible.
5
Comparing Stuff in C#
if else statements:
int myNum = 9;
if (myNum == 9) {
(this code executes if myNum is 9)
}
else if (myNum == 10) {
(this code executes if myNum is 10)
}
else {
(this code executes if none of the above is true)
}
switch (myNum)
{
case 10:
(this code executes if myNum is 10)
break;
case 12”
(this code executes if myNum is 12)
break;
default:
(this code executes if myNum is none of above cases)
break;
}
6
Loops in C#
while loops:
int i = 0;
(run code in here each time we go around and i is less than 100)
i++; (adds one to i each time we go around)
}
for loops:
These are kind of like while loops but with a little more control
7
Arrays in C#
What if you’d like to hold a list of items, all of the same type?
(Why do you write ‘1’ and not ‘2’ to get second element? Because all indexing in C# is based on
numbers starting at ‘0’ so our program counts - 0, 1, 2, 3 etc. )
8
Methods in C#
What if you want to do stuff to your variables? It would be horrible to write ‘myNum + otherNum’
EVERY time you needed it. Methods allow you to store that code in one place that can be
accessed multiple times and from multiple places.
Calling a method:
9
Classes in C#
You have variables and methods now. Where should you put them? In classes! Classes have 2
objectives - the first of which is to organise your code. C# also generally expects variables and
methods to be enclosed in classes - it’s kind of obsessive like that!
Define a class:
The second thing classes do is become blueprints for types of objects. In this case the class
holds a blueprint for a car.
toyota.PaintCar(“Green”);
ferrari.PaintCar(“Red”);
10
Get their top speeds:
Classes are mighty useful for defining new kinds of objects and VASTLY reducing code reuse.
Making objects from classes is one of the core principles of ‘Object Oriented Programming’. You
may see this abbreviated to OOP in some places.
11
Inheritance in C#
Classes don’t have to be redefined every time you make them. They can ‘inherit’ other class
properties and methods.
class Vehicle
{
int topSpeed;
string color;
}
12
Exceptions in C#
Not what you might think! An exception in C# means something happened that wasn't expected
or allowed. The result? CRASH! Your whole program gives up.
CRASH HAPPENS HERE BECAUSE ‘GRANT’ is not a number so can’t be changed to an int
If you expect something to fail you can use a ‘try’ ‘catch’ statement.
try {
int myNum = Convert.ToInt16 (myName);
}
catch (Exception e) {
(code to execute when error is thrown)
(the Exception ‘e’ contains all the details in ‘e.Message’)
}
If try fails then catch is run, giving you a chance to inform the user something is wrong - all
without an horrendous app crash!
13