c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I832 materiais10.248 seguidores
Pré-visualização50 páginas
your solution by 
using a program language is also called writing an algorithm. Programming is therefore largely a matter 
of writing algorithms, something which I will return to repeatedly.
When you have to write a program, you need a tool that can be used for entering the program code, 
and in principle one could do that with Notepad and the compiler, but in practice you will always use a 
specific development tool, because it makes the job much easier. In the following I will use throughout 
Visual Studio, that is Microsoft\u2019s general development tool for a wide variety of tasks, including writing 
code in C#. It is an integrated package that contains all the tools necessary for the development of a 
number of different program types.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
14 
Introduction
1 Introduction
C# is an object-oriented programming language. The fundamental architectural element of a program is 
a class, and from a programmer\u2019s perspective is a C# program a family of classes, that collectively define 
all the application\u2019s properties and functionality. Writing a program is thus to define \u2013 design \u2013 and write 
the code for the program\u2019s classes. Nothing in C# exists outside a class. A program will also operate by 
many other classes that are not written by the programmer, but classes that are coming from the .NET 
framework, and thus is available to the programmer as finished components.
One of the program\u2019s classes have a special role as the program\u2019s \u201centry point\u201d and the place where the 
program starts, and this class must be written with a particular naming scheme, but it is almost the only 
formal requirements for the architecture of a C# program.
Exam01
Hello World
A good place to start with a new programming language is the classic Hello World program that just prints 
a text on the screen. This program has become a mandatory part of any exposition of a programming 
language. The program can be written as follows:
using System;
namespace Exam01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
If you run the program the result is:
The program runs in a command window (prompt), where it prints the text Hello World on the screen. 
The program is not doing much, but it is a full-fledged program.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
15 
Introduction
How to
Open Visual Studio and choose File | New | Project from the menu:
Here you must be sure:
\u2022	 that you have selected the language C#
\u2022	 that you have selected the project type Console Application
\u2022	 selecting the directory where to create the program files (here F:\Home\Prog01)
\u2022	 that you have typed the program name (above Exam01)
Now when you click OK, Visual Studio will create a skeleton for an application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exam01
{
class Program
{
static void Main(string[] args)
{
}
}
}
Actually it is a full-fledged program that you can run on the machine \u2013 it made just nothing. You must 
write the program code, as shown in the introduction to this example. In this case, you only write a 
single line \u2013 a single statement:
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
16 
Introduction
static void Main(string[] args)
{
Console.WriteLine(\u201cHello World\u201d);
}
Thereafter, the program is finished and can be tested. From the menu you select
Debug | Start Without Debugging
Explanation
Note first that C# is case-sensitive, so that everywhere you have to distinguish between uppercase letters 
and lowercase letters.
Every C# program consists of at least one class, here called Program (the name chosen by Visual Studio). 
A class consists of variables and methods. In this case, the class has only one method called Main(), which 
is the method called when the program starts. A method consists of statements that can be perceived 
as commands that are performed on the machine. That a method is called means that its statements are 
executed. Note that the method Main() must be preceded by the word static. The explanation of that 
comes later. In this case, Main() has only a single statement, writing a text on the screen. WriteLine() is 
actually a method in the class Console. When the program runs, there is nothing else than the WriteLine 
statement in Main() which print a text on the screen.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
17 
Introduction
Note that in C#, every statement ends with a semicolon \u2013 above, there is a semicolon after the WriteLine 
statement. It tells the compiler where the statement ends.
In C# classes are grouped in so-called namespaces. System is a namespace that contains many classes 
including the class Console. A class\u2019s full name consists of the namespace where the class is grouped, 
and the class name, for example System.Console. In a program using defines a namespace and classes in 
this namespace can be referenced by the class name alone. Thus, one can write
Console.WriteLine("Hello World");
instead of the full name
System.Console.WriteLine("Hello World");
Visual Studio automatically inserts 5 using statements in the code and thus 5 namespaces. In this case, 
only the System namespace is needed, and you are allowed to delete the 4 others (I have done that in 
the final version shown initially of the chapter). In principle there is no particular reason to delete 
unnecessary using statements as they do not have any bearing on the final program, so the only reason 
to delete them is to make the code more readable.
Comment
Visual Studio will automatically place the program in its own namespace, here called Exam01. If you 
wrote the program using a plain text editor, it is not necessary to include this namespace. Actually the 
program can be written simpler than the above. The following version of the program is written in 
Notepad and saved as a file named Hello.cs:
class Program
{
static void Main()
{
System.Console.WriteLine("Hello World");
}
}
If you then open a .NET prompt, the program can be translated with the command
csc Hello.cs
and then forming an executable file that can be tested. All program examples in this book is written in 
Visual Studio, since the gain from bigger programs are considerable \u2013 in fact it is the only reasonable 
tool for developing .NET applications.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
18 
Basic program architecture
2 Basic program architecture
The above example shows in principle the overall architecture of a C# program which is a class that has 
a Main() method as a starting point. The example was very simple, since the program consisted of only 
a single statement in Main(). In this section I will write a program where there are several statements, 
but also several methods. In this example, there is no special justification for splitting the code into 
methods \u2013 just to show how a method is called and written in C#. Methods are useful (necessary) for 
many reasons, but partly the methods can be used to subdivide the code into more manageable parts.
Exam02
Print a book
The goal is to write a program that on the screen can print information about a book
How to
Open Visual Studio and create in the same way as in Exam01 a Console Application project. This time I 
have called the project Exam02, but otherwise all options are as above.
Visual Studio creates again a skeleton for a program, and the resulting code is shown below:
using System;