Wednesday, August 3, 2011

First Program

Thinking Through the Process



We are going to demonstrate some of the basics of writing a C# program, or any program, for that matter. One of the most common questions is where to start.

The first thing you should do is get a good grasp on the basic purpose of the program. In our case this is simple. We are going to prompt the user for a name and then display the word "Hello" and the name. In many cases, though, where the program is complex, this can be difficult. It is essential though. You really can't write a program if you don't know very clearly what the program is meant to do.

Next you should identify the inputs. Inputs are pieces of data that the program will need to complete its purpose. Inputs can come from the user, from a file or a database or from another program. In our case the input will come from the user and consist of his or her name.

Thirdly you should look at the output. Output or those things the program produces as a result of its operations. Outputs can take the form of display on the screen, files, or inserts into a database. In our case the output is the word "Hello" plus the user's name.

The fourth thing you should consider is what the process is to transform the inputs into the outputs. You should list each of the steps in the order they need to occur. This list of steps is called an Algorithm. In our case the steps are

Prompt the user to enter their name
Concatenate the word Hello and the name
Display the phrase on the console


This is obviously very simple. Many algorithms are quite complex and long.

Here is the code:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello
{
class Program
{

static void Main(string[] args)
{
string name; //variable to store name
//prompt user
Console.WriteLine("Please enter your name");
//assign value from the console to the variable
name = Console.ReadLine();
//output the word Hello and the name.
//the {0} is a place holder for the variable name
Console.WriteLine("Hello, {0}", name);

//this is just to pause it
Console.ReadKey();
}
}
}



Description of the Code


The using statements are there to include libraries of code. The System library, for instance, contains the class definition for "Console" which we use to get the input and to output the results.

Namespaces are used to group code that belongs together and to keep code separate that should be separate. By default the namespace is the name you gave to the project when you created it.

In C# all code must be in a class. That is part of its object oriented nature.

Main is a method in the class Program. It is a special method. Main is the starting point of any program. No program can have more than 1 Main. The static means it is loaded immediately into the computers memory. This is necessary because it is the starting point. We will talk more about static later. void means the method does not return anything. Again we will talk about this later. string[] args creates and array that can be used to pass into from the command line. We will not be using this.

Note the curly braces {}. Each block has its own pair of opening and closing curly braces. The namespace is a block, the class is a block and the main method is a block. Leaving out a curly brace or inserting an extra one is a common mistake that can keep your program from compiling.

string name; declares a variable of the type string called "name". A semicolon ; terminates the command. All commands in C# must be ended with a semicolon.

statements beginning with // are comments. I have commented what each line does.

Notice the dot in Console.WriteLine(). Console is an object that belongs to the .Net framework. The dot introduces a member of that object, in this case, a method "WriteLine." We will see this "dot notation" in every program.



Review of Process


Once again these are the steps to follow in creating any program:

1) Get a clear overview of the program's purpose
2) Identify the program's inputs
3) Identify the programs outputs
4) determine the steps required to get from the inputs to the outputs

No comments:

Post a Comment