Vous êtes sur la page 1sur 4

Exercises

1. Install and make yourself familiar with Microsoft Visual Studio and Microsoft
Developer Network (MSDN) Library Documentation.
2. Find the description of the System.Console class in the standard .NET API
documentation (MSDN Library).
3. Find the description of the System.Console.WriteLine() method and its
different possible parameters in the MSDN Library.
4. Compile and execute the sample program from this chapter using the
command prompt (the console) and Visual Studio.
5. Modify the sample program to print a different greeting, for example "Good
Day!".
6. Write a console application that prints your first and last name on the
console.
7. Write a program that prints the following numbers on the console 1, 101,
1001, each on a new line.
8. Write a program that prints on the console the current date and time.
9. Write a program that prints the square root of 12345.
10. Write a program that prints the first 100 members of the sequence 2, -3, 4,
-5, 6, -7, 8.
11. Write a program that reads your age from the console and prints your age
after 10 years.
12. Describe the difference between C# and the .NET Framework.
13. Make a list of the most popular programming languages. How are they
different from C#?
14. Decompile the example program from exercise 5.

Solutions and Guidelines


1. If you have a DreamSpark account (www.dreamspark.com), or your school or
university offers free access to Microsoft products, install the full version of
Microsoft Visual Studio. If you do not have the opportunity to work with the full
version of Microsoft Visual Studio, you can download Visual Studio Express for
free from the Microsoft web site; it is completely free and works well for educational
purposes.
2. Use the address given in the ".NET Documentation" section of this chapter.
Open it and search in the tree on the left side. A Google search will work just as
well and is often the fastest way to find documentation for a given .NET class.
3. Use the same approach as in the previous exercise.
Chapter 1. Introduction to Programming 109

4. Follow the instruction from the Compiling and Executing C# Programs section.
5. Use the code from the sample C# program from this chapter and change the
printed message.
6. Find out how to use the System.Console.Write() method.
7. Use the System.Console.WriteLine() method.
8. Find out what features are offered by the System.DateTime class.
9. Find out what features are offered by the System.Math class.
10. Try to learn on your own how to use loops in C#. You may read about forloops in the chapter Loops.
11. Use the methods System.Console.ReadLine(), int.Parse() and
System.DateTime.AddYears().
12. Research them on the Internet (e.g. in Wikipedia) and take a closer look at
the differences between them. You will find that C# is a programming language
while .NET Framework is development platform and runtime for running .NET
code. Be sure to read the section The C# Language and the .NET Platform
form this chapter.
13. Find out which are the most popular languages and examine some sample
programs written in them. Compare them to C#. You might take a look at C, C++,
Java, C#, VB.NET, PHP, JavaScript, Perl, Python and Ruby.
14. First download and install JustDecompile or ILSpy (more information about
them can be found in the Code Decompilation section). After you run one of them,
open your programs compiled file. It can be found in the bin\Debug subdirectory
of your C# project. For example, if your project is named TestCSharp and is
located in C:\Projects, then the compiled assembly (executable file) of your
program will be the following file
C:\Projects\TestCSharp\bin\Debug\TestCSharp.exe.

using
using
using
using

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

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//5. Modify the sample program to print a different greeting, for example "Good
Day!".
//Console.WriteLine("Good day!");

// 6. Write a console application that prints your first and last name on the
console.
string _FirstName;
string _lastName;
Console.WriteLine("Please Enter your First Name : ");
_FirstName = Console.ReadLine();
Console.WriteLine("Please Enter your Second Name : ");
_lastName = Console.ReadLine();
Console.WriteLine("Your Full name is {0} {1} ", _FirstName, _lastName);
Console.Read();
//7. Write a program that prints the following numbers on the console 1, 101,
1001, each on a new line.
//Console.WriteLine(1);
//Console.WriteLine(101);
//Console.WriteLine(1001);
//Console.Read();
//8. Write a program that prints on the console the current date and time.
// DateTime now = DateTime.Now ;
//Console.WriteLine(now);
//Console.Read();
//

9. Write a program that prints the square root of 12345.


//double _Sqrt = Math.Sqrt(12345);
//Console.WriteLine("The Square root of 12345 is {0} : ", _Sqrt);
//Console.Read();

//10. Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6,
-7, 8.
for (short i = 1; i < 100; i++) // to match the requirement perfectly it must be:
(short i = 2; i < 12; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
else
{
Console.WriteLine(-i);

}
}
Console.Read();

}
//11. Write a program that reads your age from the console and prints your age after 10
years.
Console.WriteLine("Please enter your Age : ");
int age = int.Parse(Console.ReadLine());
int Nage = age + 10;
Console.WriteLine("Your Age after ten years would be : {0} Years ", Nage);
Console.Read();

Vous aimerez peut-être aussi