After the dryness of chapter 1, we start getting into the building blocks. Yay! This is mostly maths and basic methods of storage, as well as a few pointers on naming conventions and the like. I don’t recall getting stuck on any of this stuff before, so it should be a breeze to explain.

Comments

Firstly, use comments. They’re so useful when you start because you can comment each line and explain what you are doing. As you get more knowledgeable you will move from how that line of code works to why you chose to write it like that.

There are a few types of comment:

In-line Multi-line Documentation
// this is a comment
/* This is
a
comment /*
/// <summary>
/// This is a summary.
/// <remarks>
/// This is a longer comment.
// This is
// a
// comment

The documentation type of comment will generate an XML file for you when you compile your program. I guess this is useful for generating a how-to guide for your code.

Basics of a C# Program

C# is made up of keywords, literals and identifiers. They do everything from telling a value to stay the same, to what type of value something is. There are in-built keywords that you’ll use and you’ll make your own identifiers as you go along.

Something really important that you’ll need to know for any of your programs is that you tell your computer to start the program with Main(). This has be half way down your program, buried amongst other code, but this is always where your program will start from.

You store information in a program by using variables. These are areas of your computer’s memory for storing numbers, words, sentences etc. You have to be careful when you’re naming variables as C# keywords can’t be used. You also can’t use special characters such as # or @. It’s also a good idea to name your variables after the things they are storing. If you’re going to store your age in a variable, myAge or age is a lot easier to remember than variable1. I prefer to use lower CamelCase for my variables.

You should always assign a value to your variables, even if this is temporary and will be changed later. If you don’t, this can cause your program not to work.

For numbers, there are several types of variable that you can use, all allowing for different sizes of data to be stored in them. You should think about whether your numbers can be negative or not, and whether they will be whole numbers as this will also affect your choice of data type.

Data Type Size (bytes) Lowest Value Highest Value
Whole Numbers
sbyte 1 -128 127
byte 1 0 255
short 2 -32,768 32,767
ushort 2 0 65,535
int 4 -2,147,483,648 2,147,483,647
uint 4 0 4,294,967,295
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
ulong 8 0 18,446,744,073,709,551,615
Decimal Numbers
float 4 1.5×10-45 3.4×1038
double 8 5.0×10-324 1.7×1010308
decimal 16 1.0×10-28 ~7.9×1028
bool 1 false (0) true (1)

The book goes on about computer memory and how it works in this section – that’s the bytes in the table above. To be perfectly honest, while it helps to know about it, all you need to know at this stage is that you should use the smallest versions of numbers that you can in order to save memory. Think of it as shopping. You wouldn’t pick the biggest bag to carry a box of eggs in; you’d use the smallest one that the shop had available that was big enough to fit the eggs in to. It’s not something you really need to concern yourself with when you’re starting out, though. Just something to bear in mind.

There are other variable types that will be useful (you don’t always want to store numbers, after all). The type char, for example, lets you display special characters such as &, @ or smiley faces (which any coding language generally don’t like) using numbers. Char = 63 would show ? whereas char = 64 would show @. I use this list when I’m after special characters for my web design, but it is also useful for this. For the C# char values, you’ll want the “dec” or decimal column. The data type string lets you store text such as a sentence, or name.

Sometimes, the value you want to store in a variable isn’t the same as it’s data type. For example, 99.9 will be assigned as double by default. But what if you want to put it in a float? Well, you’d use 99.9f instead. If you wanted to store it as a decimal you’d write 99.9m.

Sometimes you don’t want a number to change at all. In this case you’d set it as a constant or const. So, using the value of PI we’ve used previously, the value of PI never changes. So, to represent this we would declare this variable as const float PI = 3.1459.

Terms

https://jambonium.co.uk/glossary/data-type/
https://jambonium.co.uk/glossary/declaration/
https://jambonium.co.uk/glossary/expression/
https://jambonium.co.uk/glossary/extensible-markup-language-xml/
https://jambonium.co.uk/glossary/identifier/
https://jambonium.co.uk/glossary/keyword/
https://jambonium.co.uk/glossary/literal/
https://jambonium.co.uk/glossary/statement/
https://jambonium.co.uk/glossary/white-space/
https://jambonium.co.uk/glossary/variable/