Software

C# Tutorial For Beginners

Software Basics - Part 1

*Some things are common to pretty much all software development. The fundamentals are explained here and will help to understand the later sections. You can come back to this section later if you need to so there is no need to memorise everything.*

Numbers in computers are stored using switches that can be either on or off. To do this it makes sense to use the binary number system, which only includes the digits 0 and 1 which maps nicely onto the 'on' and 'off' states of a switch. A 1 is represented by a switch being on and a 0 by a switch being off.

To count in binary is similar to counting like we normally do in decimal but instead of carrying over and adding 1 to the next column when reaching 10, we do it after reaching 2.

So, 0 in binary is 0, the same as in decimal.

Adding another 1 gives us 1.

Then adding another 1 means we need to carry over to the next column and we get 10.

Here's a list of numbers 0 to 12 in decimal then binary:

Decimal | Binary |
---|---|

0 | 0 |

1 | 1 |

2 | 10 |

3 | 11 |

4 | 100 |

5 | 101 |

6 | 110 |

7 | 111 |

8 | 1000 |

9 | 1001 |

10 | 1010 |

11 | 1011 |

12 | 1100 |

Rather than units, tens, hundreds, thousands etc. columns there are units, twos, fours and eights columns.

Each digit in a binary number is called a 'bit' which is short for 'binary digit'. When 8 bits are combined together they are called a 'byte', which can represent a number between 0 and 255. 1024 bytes are referred to as a kilobyte.

Bytes can be joined together to make larger numbers, effectively giving more digits to represent the value. 16 bits can represent a number from 0 to 65535, which is 65536 possible values.

In order to represent negative numbers, 1 bit of a binary number can be used to represent the sign. This doesn't necessarily mean there is 1 digit less to store a number as you will see later.

The names given to these numbers are called 'types' and depend on the computer architecture and software being used. In C# the following types are used:

Type Name | Description |
---|---|

byte | A value between 0 and 255 |

sbyte | A value between -128 and 127 |

ushort | A value between 0 and 65535 |

short | A value between -32768 and 32767 |

uint | A value between 0 and 4294967295 |

int | A value between -2147483648 and 2147483647 |

ulong | A value between 0 and 18446744073709551616 |

long | A value between -9223372036854775808 and 9223372036854775807 |

float | A value between -3.402823e38 and 3.402823e38 |

double | A value between -1.79769313486232e308 and 1.79769313486232e308 |

decimal | A value between -79228162514264337593543950335 and 79228162514264337593543950335 |

char | Represents a unicode character |

string | A number of chars together |

bool | Can be set to true or false |

object | A type that can be defined by the programmer |

All types of computer memory store data in a way that is accessed via an 'address'. For example a 1 kilobyte block of RAM can have 1024 locations that are addressed with a number. When writing software, rather than remembering the location of a piece of data that you want to use a name is given to it. The names are known as 'variables', because the value can be changed by the software.

Each variable in C# has a type and must be 'declared' before it can be used. This is done by writing its type followed by a name that you want to give it.

For example to declare a byte called MyByte you would write the following:

byte MyByte;

The ';' just means it is the end of the statement.

An expression in software is just a calculation using a number of variables or numbers. When an expression is performed, the result can be stored in another variable.

You can set the value of a variable using a number as follows:

MyByte = 10;

Or, if you have declared another variable as follows:

byte MyOtherByte;

You can then set it be the same as another variable:

MyOtherByte = MyByte;

Here are some other examples of expressions that you can use:

Expression | Description |
---|---|

MyOtherByte = 2 + 2; | Add 2 and 2 together and store the result in MyOtherByte |

MyOtherByte = MyByte + 2; | Add 2 to MyByte and store the result in MyOtherByte |

MyOtherByte = MyOtherByte - 100; | Subtract 100 from MyOtherByte and store the result in MyOtherByte |

MyByte = MyByte + MyOtherByte - 4; | Add MyByte to MyOtherByte then subtract 4 |

MyByte = MyByte + (MyOtherByte * 3); | Multiply MyOtherByte by 3 then add MyByte an then store the result in MyByte |

Operators are the symobols that are used to perform calculations on the variables. As well as the familiar mathematical operators such as + and -, there are many others available.

Here are a few examples:

Operator | Description |
---|---|

+ - * / | Add, Subtract, Multiply and Divide |

++ -- | Increment, Decrement, e.g. a=a++ is the same as a=a+1 |

- | Negation, e.g. a=-a |

<< >> | Shifts the bits in a variable left or right a number of times, e.g. if a=2, (binary 10) a=a<<1 gives a result of 4 (100) in binary |

This is just a small selection of some of the most common operarors, to see a full list of operators, see Microsoft's website here.

Previous Next