Vous êtes sur la page 1sur 7

Chapter Five

5
The Structure of a C# Program

After this chapter you should

understand the role of Visual Studio in programming

be able to develop a simple program outside Visual Studio and


compile it

understand the basic structure of a C# program

understand the role of namespaces in a C# program

understand how a C# program is saved

Key concepts
Visual Studio
Main() method
Namespaces
Chapter 5 2 Structure of a C# Program

The role of Visual Studio


We must remember that C# is not equal to Visual Studio. The programming language is C#
while Visual Studio is just the environment in which the program is developed to make it easier
for us. In fact, it is possible to write a C# program outside of Visual Studio and then compile
and run it.

Things to do

Open Notepad or any other plain text editor and type the following lines of code. Save the
file with a .cs extension, e.g. Hello.cs.

class Hello
{
static void Main()
{
System.Console.WriteLine("Hello World!");
System.Console.Write("Press any key to exit ...");
System.Console.ReadKey();
}
}

Open a command window:

Click on Start / Run …


Type cmd in the text box and press OK.

Change the directory so that the folder


where your program file is saved is the
active folder. In the screen print below,
the file Hello.cs is saved in the
C:\BeSharp\Hello World directory.

(Hint: cd is a command for "Change directory". cd\ means that the directory (folder) must
be changed to the root directory of the current disk. cd BeSharp\Hello World means that the
directory must be changed to C:\BeSharp\Hello World.)

Type the full path and file name of the C# compiler in the .NET framework (csc.exe)
followed by a space and then the name of the file that you want to compile. See the
screen print below for an example.

If you press Enter now, you should see a message about the compiler version that was
used.
Chapter 5 3 Structure of a C# Program

Open the folder where your program is saved in Windows Explorer and check that a .exe file
was created.

Run the .exe file (double click on the file) and check that the program is working as you
expected.

Instead of running csc from a command prompt, you may create a batch file.
- Open a new text file in Notepad and enter the line of text as in the screen print below.
Save the file as "Compile.bat" in the same folder where your "Hello.cs" file is. To
compile "Hello.cs" you can now only double click on "Compile.bat" from within Windows
Explorer.

Things to understand

You can develop a C# program in any text editor. You can even do it in MS Word as long
as you save the file as plain text with a .cs extension.

A text editor, such as Notepad, does not provide IntelliSense or other environmental tools
to make your life as a programmer easier.

The C# compiler comes free of charge as part of the .NET framework. In other words, we
don't need Visual Studio to write and compile a C# program.

Note that semicolons are only added behind the actual program statements in the Main()
method. No other program lines have semicolons.

The Basic Structure of a C# program


The program listing below shows the minimum elements that any C# program should have.

class Hello
{
static void Main()
{
//Enter program code here
}
}
Chapter 5 4 Structure of a C# Program

All programming code should be written within a class. Every class should have a name.
In the above example, the name of the class is Hello.

All programs should have a Main() method. Program execution always starts with the
Main() method.

The Main() method should be declared as static, meaning that it would not be necessary
to instantiate an object of the Hello class.

Every method should always indicate a return type. If the method does not return a
value as in this case, it should be indicated with the keyword void.

Namespaces
Classes can be grouped into bundles, referred to as namespaces. In a line such as
System.Console.WriteLine("Hello World!");

Namespace Class Method Parameter

the word System refers to a namespace that contains the Console class. We can add a
using directive (this is not a statement) before a class to indicate to the compiler that it
should search for a class in a specific namespace. In other words, we can change the
program above to limit the amount of typing and, to some extent, also enhance readability:

using System;
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
Console.Write("Press any key to exit ...");
Console.ReadKey();
}
}

Note that there is a semicolon behind the using directive.


We can also define our classes inside a namespace, for example:

using System;
namespace Hello
{
class Hello
{
static void Main()
{
System.Console.WriteLine("Hello World!");
System.Console.Write("Press any key to exit ...");
System.Console.ReadKey();
}
}
}

Open the last console application that you developed in Visual Studio again and inspect the
code that was generated by Visual Studio. You should be able to understand the structure
of the program better now.
Chapter 5 5 Structure of a C# Program

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

namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
}
}
}

Note that, besides the System namespace, Visual Studio also added some other
namespaces in using directives. Visual Studio has no way to tell in advance which classes
you will be using in your program and included the most common namespaces by default.
Depending on the classes that you need, you may delete some of these using directives
without any effect on the program.

For the time being, you can ignore the arguments between the brackets of the Main()
method. We will discuss that at a later stage.

The file structure of a C# program


Open Windows Explorer and browse to the folder where you saved the program that you
developed in a text editor.

You should see only two files, namely the original text file with a .cs extension (cs = "C
Sharp") and the .exe (executable) file. The .exe was generated by the csc compiler and it
is this file that can be distributed to users.

Again, in Windows Explorer, browse to the last console application that you developed in
Visual Studio. You will notice many more files that Visual Studio generated as part of the
project.

Project file

Solution folder
Project folder
Chapter 5 6 Structure of a C# Program

- Visual Studio allows the grouping of one or more projects into a solution. All files of a
solution is saved in a solution folder. The .sln (solution) file organizes all project items
and keeps track of file names and their locations on disk. This is the file that must be
opened when you want to return to an existing solution.

- The .csproj (C Sharp project) file keeps track of forms and classes in your project and
their locations on disk. Since we are mostly working with solutions that contain a single
project, you can open the .csproj file with the same effect as opening the .sln file. This
file is normally located in the project folder, which is a sub-folder of the solution. It will
often happen that the solution and the project have the same name and therefore the
solution folder and the project folder will have the same name.

- The .cs (C Sharp) files contain the C# program code. These files are normally located
in the project folder, which is a sub-directory of the solution directory.

- The /Bin/Debug sub-folder contains an .exe (executable) file which can be executed
outside of Visual Studio. Note that you cannot distribute this file to users who do not
have Visual Studio installed.

- If you want to distribute a .exe to users who do not have Visual Studio, you should
select Release in the Solution Configurations dropdown box (see screen print below).
When you run the program, a /Bin/Release sub-folder will be created which contains a
.exe file that can be distributed.

The file structure of Windows Forms applications that are created in Visual Studio looks
very similar to that of console applications. There are, however, some differences.

- One of the .cs files (called Program.cs unless you changed its name) contains the
Main() method. This method contains a line of code that will instantiate and display
the first (or only) form in the program. The contents of this file were generated by
Visual Studio.

- Visual Studio will create a class for every form that is added to the project. A form
class is split over two files and every file will thus contain a partial class.
One of the files (extension .cs) contains the C# code that you entered for the event
handlers.

The other file (extension .designer.cs) contains C# code to define the form and the
graphical components on the form. This code is generated automatically by Visual
Studio as you drag and drop controls from the toolbar and set their properties. It is
normally not good practice to tamper with the contents of this file.
- Later, when you develop your own classes, they will also be saved in files with a .cs
extension.

- Note that all classes that belong to the same project will be part of the same
namespace.
Chapter 5 7 Structure of a C# Program

Keywords
You should make sure that you know what each of these items mean or where they are used.

.cs csc Project


.csproj Directory return type
.designer.cs Executable file Release version
.exe File structure Solution
.sln Folder static
C# Main() System namespace
Change directory Method using
class Namespace Visual Studio
Class Parameter void
Compiler partial class Windows Explorer
Console Path

Key:

Concepts : Normal
Classes and Controls : Green, e.g. Color
Methods : Bold with brackets, e.g. Main()
Reserved words : Blue, e.g. class

Exercise
1. Write a C# program in a text editor such as Notepad to allow the user to enter the
names and test marks for 5 students. The program should then display a complete
class list with the class average to one decimal digit. Output should be properly aligned
as in the example below.

(You may think about doing it in Visual Studio first to sort out all the errors and then
copy and paste it into Notepad. Don't do it, this way you would not get the feeling of
doing it in a "dumb" environment.)

Vous aimerez peut-être aussi