The Basic Structure of C# Programs - C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

Chapter 2. The Basic Structure of C# Programs

This chapter will teach you the fundamental structure of C# programs. This information can help you achieve your goal (i.e. to master the basics of C# within 2 weeks).

Writing Your First Program

C# programs are composed of these parts:

· Classes

· Comments

· Expressions and Statements

· Namespace Declarations

· Class Attributes

· Main Methods

Analyze the screenshot below. It is an easy code that prints this phrase:“Hello World”

Once you have compiled and executed the code above, you’ll get this statement:

Now, let’s analyze the parts of that simple code:

· using System; - “using” is a keyword that can link namespaces with programs. In general, a single program contains many using statements. In this case, using is employed to attach System (i.e. a namespace) onto the program.

· namespace– This is the code’s second line. Basically, a namespace is a group of different classes. In the example above, HelloWorldApplication (a namespace) contains HelloWorld (a class).

· class– This line is called“class declaration.” It indicates that HelloWorld holds the information needed by the program. In general, classes hold various methods. In this situation, however, the class contains a single method (i.e.“Main”).

· Main– This method serves as the entry point of all C# codes. Main specifies the capabilities of the class it belongs to.

· /**/ - The C# compiler ignores this line. You should use it if you want to add some comments into your program.

· Console.WriteLine(Hello World); - Main uses this statement to specify the behavior of the class.

o WriteLine - This method belongs to a class named Console. WriteLine makes your screen display the code’s message (i.e. Hello, World).

· Console.ReadKey(); – This line is designed for users of VS.NET (i.e. Visual Studio .NET). Console.ReadKey() stops the screen from closing if you’ll launch the program using VS.NET.

Here are four things that you should remember:

· You don’t have to use the class name as the filename for your program.

· The execution of the program begins at Main.

· Each statement and expression must be terminated using a semicolon.

· The C# programming language is case-sensitive.

How to Compile and Execute a C# Program

Using Visual Studio .NET

Here are the steps you need to take if you are using VS.NET:

1. Access Visual Studio.

2. Go to the menu bar and select“File”. Then, click on‘‘New” and“Project”.

3. The screen will show you several templates. Look for“Visual C#” and select Windows.

4. Select“Console Application.”

5. Assign a name for the new project and hit“OK.” This action will create a project within Solution Explorer.

6. Enter the code into VS.NET’s Code Editor.

7. Run the program by pressing F5 or hitting“Run.” For the current example, a new window will display“Hello, World.”

Using the Command Line

You may also use the command line to compile your C# programs. Here are the things you need to do:

1. Access your favorite text editor.

2. Enter the code given above.

3. Save the file into any directory and name it hw.cs.

4. Access your computer’s command prompt and specify the directory you chose for Step 3.

5. Compile the code by typing csc hw.csand hitting the“Enter” key.

6. If you entered the code correctly, you will see an executable file named hw.exe.

7. Type hw to run the program.

8. The screen should display“Hello, World.”