Programming with C# - - 101
Preparing to Program
You should take certain steps when you're solving a problem. First, you must define the problem. If you don't know what the problem is, you can't find a solution! After you know what the problem is, you can devise a plan to fix it. When you have a plan, you can usually implement it. After the plan is implemented, you must test the results to see whether the problem is solved. This same logic can be applied to many other areas, including programming.
When creating a program in C# (or in any language), you should follow a similar sequence of steps:
- Determine the objective(s) of the program.
- Determine the methods you want to use in writing the program.
- Create the program to solve the problem.
- Run the program to see the results.
The second step is to determine the method you want to use to write the program. Do you need a computer program to solve the problem? What information needs to be tracked? What formulas will be used? During this step, you should try to determine what will be needed and in what order the solution should be implemented.
As an example, assume that someone asks you to write a program to determine the area inside a circle. Step 1 is complete, because you know your objective: Determine the area inside a circle. Step 2 is to determine what you need to know to ascertain the area. In this example, assume that the user of the program will provide the radius of the circle. Knowing this, you can apply the formula r2 to obtain the answer. Now you have the pieces you need, so you can continue to steps 3 and 4, which are called the Program Development Cycle.
The Program Development Cycle
The Program Development Cycle has its own steps. In the first step, you use an editor to create a file containing your source code. In the second step, you compile the source code to create an intermediate file called either an executable file or a library file. The third step is to run the program to see whether it works as originally planned.
Creating the Source Code
Source code is a series of statements or commands that are used to instruct the computer to perform your desired tasks. As mentioned, the first step in the Program Development Cycle is to enter source code into an editor. For example, here is a line of C# source code:
This statement instructs the computer to display the message Hello, Mom! onscreen. (For now, don't worry about how this statement works.)
Using an Editor
An editor is a program that can be used to enter and save source code. There are a number of editors that can be used with C#. Some are made specifically for C#, and others are not.
At the time this book was written, there were only a few editors created for C#; however, as time goes on, there will be many more. Microsoft has added C# capabilities to its Visual Studio product which includes Visual C#. This is the most predominant editor available. If you don't have Visual Studio .NET, however, you can still do C# programming.
Page 1 of 5