Posted 12 years ago
·
Author
In this tutorial I am going to show you a simple program which we can break down and analyse to learn about how C# programs are stuctured. If you haven't ever compiled a C# program, refer to my guide on installing and using Microsoft Visual C# 2010 Express.
To start off we must understand what is the point in making your own programs? It's simple; programs make our life easier. A computer can make calculations much faster than the human brain and although it will take us longer to build a large program, it will save us time in the long run! In our lesson we are going to imagine a little scenario in which we might need such knowledge of programming.
You have decided to start fencing business - one that designs fences for peoples gardens. The only problem is that you have a bad memory and can never remember calculations. You decide that if you make a program with the correct calculation, you will need only write it once and it will do the work for you forever. What this program must do is prompt the user for the length and width of a garden and then work out how much wood is needed. You are going to assume that the height of every fence is 2m.
Start a new blank project and create a source file with the following in it:
/*
*First program of the series
*Prompts user for dimensions and calculates an area
*/
using System;
class FenceAreaCalculator
{
static void Main()
{
const int fenceHeight = 2;
double gardenWidth, gardenLength, gardenPerimeter;
string gardenWidthS, gardenLengthS;
double fenceArea;
Console.WriteLine("Width of garden:");
gardenWidthS = console.ReadLine();
gardenWidth = double.Parse(gardenWidthS);
Console.WriteLine("Length of garden:");
gardenHeightS = console.ReadLine();
gardenHeight = double.Parse(gardenLength);
//Work out the area of the fence needed
gardenPerimeter = 2 * ( gardenHeight + gardenLength );
fenceArea = gardenPerimeter * fenceHeight;
console.WriteLine("The area of fence needed for the garden is " + fenceArea + " square meters");
}
}
In this program, we have successfully achieved our objective. Now it's time for you to understand it! The first line is optional but it's what helps us save a lot of time in programming.
This line is what we call a using directive in C#. It is used to tell our compiler what namespaces will be used to save us from using fully qualified names in our source code. Thanks to this, we can write Console.WriteLine instead of System.Console.WriteLine. Another inportant part to notice is that it ends with a semicolon (;). This is important to remember. A programming statement will always end with a semicolon to tell the compiler it is the end of the statement.
The next part we will look at is the block of code known as the class.
This block of code is known as a class - a construct that allows us to create customs types. A class is always declared with the keyword class followed by the name of the class, in this case we decided to call it FenceAreaCalculator. As the class contains a block of code, it must open and close with braces (curly brackets). The inside of the class will always define the behaviour and data of this type. Classes are what make a language object-oriented, which means the language includes (but is not limited to):
- Encapsulation
- Polymorphism
- Inheritance
These will all be covered in a later lesson.
The previous block of code is referred to as a method. We can explain this by just reminding ourselves that programs are usually made to solve a problem. The method is the method by which we can solve these problems. Inside it you will find other method calls, declaration of variables, maths, algorithms and more! It all depends on how complex your program needs to be. To declare a method you type the return type and the function name. Since we are working with console programs, the entry point to the program must be called Main. In this case we have given it a type of void, meaning it doesn't return a value but this will be covered in more detail later. After the function name there must be a pair of parentheses containing the arguments for that method which we have left blank for now. The static keyword and the arguments will also be covered at another time.
const int fenceHeight = 2;
double gardenWidth, gardenLength, gardenPerimeter;
string gardenWidthS, gardenLengthS;
double fenceArea;
These lines are what we call variable declarations. They are the names and data types of our variables - placeholders for values. If you have ever done algebra at school, you can compare this to calling a value x or y, characters or words that represent values.
Console.WriteLine("Width of garden:");
gardenWidthS = console.ReadLine();
gardenWidth = double.Parse(gardenWidthS);
These three lines are easily enough understood. The first line is a method call that can print strings to the console (as well as other data types such as integers). Just like the Main method, the WriteLine method takes arguments in parentheses. In this case it takes a string (surrounded by quotation marks). The second line calls the console.ReadLine() method and assigns it's returned value to a variable - gardenWidthS. The only problem is that WriteLine and ReadLine both only deal with strings which is something we can't do maths with (technically we can but this would involve working with a base 36 numerical system and I'm not here to teach that). Don't believe me? Try adding potato and cucumber in the calculator.
The way we can get around this is by converting the variable into another type, this is known as parsing. We are going to change from a string to a double, so we use double.Parse(the variable name). Now that we have numerical values we can do maths on it.
//Work out the area of the fence needed
gardenPerimeter = 2 * ( gardenHeight + gardenLength );
fenceArea = gardenPerimeter * fenceHeight;
In this bit of code we are assigning a value to a variable from the mathematical equation on the right. Mathematical operations will be covered in greater detail later. You may have noticed the bit of text beginning with a double forward slash on the first line. It looks like normal English so it can't be code. Well you're right, it isn't. This is what is called a comment in C#. It is a way of writing notes in our source code to improve the readability or to help others understand it. There are two ways of doing it, both of which are below:
Comments are always ignored by the compiler so you may write as many as you want in!
console.WriteLine("The area of fence needed for the garden is " + fenceArea + " square meters");
This last line shows the last part of our code which shows concatentation within the WriteLine method. To concatentate means to join together. In this line we are combining a two strings along with a variable in one method instead of writing more than one line. As you can imagine this is useful for making the program look nicer and reducing (slightly) the work load.