Introduction to C#: Writing Code



The programs you will write are meant to give instructions to the computer about what to do, when to do something, and how to do it. You write these instructions in an easy to understand English format, using words we will study. This means that a regular instruction uses normal text with alphabetic characters, numbers, and non-readable symbols. Normally, you can write your instructions using any text editor such as Notepad, WordPad, WordPerfect, or Microsoft Word, etc. When writing your instructions, there are rules your must follow and suggestions you should observe. We sill study each one of them as we move on.

The group of instructions used by your program is also referred to as code. To assist you with writing code, Microsoft Visual C# 2005 Express Edition includes a text editor referred to as the Code Editor. This is the window that displays when you have just created a console application. Besides the Code Editor, the integrated development interface (IDE) of the Microsoft Visual C# 2005 Express Edition is made various parts, which we will review when necessary.

Practical LearningPractical Learning: Creating a Program

  1. Start Microsoft Visual C# 2005 Express Edition or Microsoft Visual C# 2005 Professional
  2. To create a new application, on the Start Page, on the right side of Create, click Project
  3. In the Templates section, click Console Application
  4. Change the Name to GeorgetownCleaningServices1 and click OK


A comment is a line or paragraph of text that will not be considered as part of your code of a program. There are two types of comments recognized by C#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between this combination of /* and */ would not be read. Therefore, you can use this technique to span a comment on more than one line.


Practical LearningPractical Learning: Creating Comments

  1. To create comments, change the file as follows:
    using System;
    // using System.Collections.Generic;
    // using System.Text;
    namespace GeorgetownCleaningServices1
        class Program
            static void Main(/*string[] args*/)
  2. To save the project, on the Standard toolbar, click the Save All button
  3. Accept the name as GeorgetownCleaningServices1 and click Save

Code Colors

Code is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text. The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.


Indentation is another feature that makes your program easy to read. Indentation is a technique of grouping lines of code by category. To delimit the items of your code, you should indent them by two empty spaces or one tab. Indentation should be incremental. That is, when a line of code appears to be a child of the previous line, the new line should be indented.


Home Copyright © 2006-2007 FunctionX, Inc. Next