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.
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:
// 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,
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 variable type.
|Data Type||Size (bytes)||Lowest Value||Highest Value|
|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.
These terms are written as I understand them at the time of writing this blog. I may come to expand on them, or change them completely as I learn more about programming. You can find an up-to-date list of the terms on my programming terms page.
Extensible Markup Language (XML) – Another language and nothing to do with C#. You can read more about it on Wikipedia if you like, but it’s not really required reading (and, like most Wikipedia articles, filled with techno babble 😉 ).
White space – This is a term that covers any spaces in your source code. Tabs, carriage returns, spaces etc. C# doesn’t read it so you can format your code however you like. This makes it readable for you, and doesn’t change how your computer reads it.
Keywords – Words that have a specific meaning within C#.
Literals – Values that are what they say they are. This is a bit odd to explain. The book says they are hard-coded values – when you specifically state something. So, for example, you would state that
PI is 3.14. It will never change, this is the accepted value of PI. Text is literal too. It will never mean anything else.
Identifiers – Another awkward thing to explain. This term is for items that have a name. So my identifier is Michelle-Louise; my name. The term PI is an identifier too.
Expressions – The book describes this well. An expression is a phrase. It’s a snippet of code that is made up of keywords. So
PI = 3.14159 is an expression. As is
PI * radius * radius.
Statements – Again, the book describes this in plain English. A statement is like a sentence. So
Area = PI * radius * radius; would be a statement.
Declarations – The techno babble explanation is that you would declare an identifier and assign a literal to it. Basically this means you’re telling your program you want to make something called
radius and that you want to store a number in. So by saying
int radius = 59; you’ve declared the identifier
radius and said it should be of the type int (the keyword for an integer, or number for those who don’t speak maths). You’ve also assigned the number
59 to it.