Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

86 C# PRIMER PLUS

Under the headline Console.ReadLine Method, you can read a short description of ReadLine. You can locate the WriteLine method in the same fashion.

Many of the terms mentioned in the .NET Framework Reference will not make any sense to you right now. However, eventually this reference is most likely going to be an important source of information.

As you are presented with more classes from the library, try to locate them and use the new terms introduced in the coming chapters to familiarize yourself with this very powerful set of tools.

C# Statements

As mentioned earlier, each method in C# contains a collection of statements. Many types of statements exist in C#. The following is a brief summary of the statements we encountered in Listing 3.1.

Declaration statements (line 7)

Assignment statements (line 11)

Method call statements (lines 9, 10, 11, 13, and 14)

Declaration statements create a variable that can be used in the program. They announce the type and identifier of the variable.

Even though declaration statements may seem superfluous, they introduce several features that help eliminate bugs in computer programs.

An assignment statement uses the assignment operator =. It assigns a value to a storage location that is represented by the identifier of the variable.

A method call activates a method. It can send arguments to the invoked method that utilizes these to perform its actions. When the invoked method terminates, the program will return to the statement immediately following the method call statement.

Summary

In this chapter, you have learned about abstraction and encapsulation, two important objectoriented concepts. You have also been presented with a simple C# program and learned about its basic C# elements and general underlying concepts.

The following are the important points covered in this chapter:

Abstraction and encapsulation are important concepts in object-oriented programming.

Abstraction allows programmers, through simplification, to cope with complexity. Only attributes relevant to the problem at hand should be included in an abstraction.

Encapsulation packs data and their associated actions into the class entity. Only relevant parts of an object are exposed to the outside world. This keeps the data inside an object

Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 87

non-corrupted and provides for a simpler interface to the outside world. public and private are two important C# keywords used when implementing the encapsulation concept.

The method of one object can call the method of another object. In OO terminology, we say that a message is sent between two objects.

A class is written in the source code and is an inactive blueprint for its dynamic object counterparts that are created inside the main memory during the execution of a program.

Comments are ignored by the compiler but are used to make the code clearer for the person reading through the source code.

The keywords in C# were chosen by C#’s designers so their names and predefined meaning never change. In contrast, identifiers vary between programs, are decided by the programmer, and are used to name C# constructs, such as classes, methods, and instance variables.

A block forms a logical unit in a program. Among other functions, blocks are used to indicate which parts of a program belong to which classes and methods.

Every program must have one Main method. The Main method is called by the .NET runtime and is the first part of a program to be executed.

A variable has a name (identifier), is of a specific type, and represents a memory location containing a value.

A variable of type string can be used to store text.

Any set of actions performed by C# can be broken down into simple instructions called statements.

The predefined functionality in the .NET Framework class library can be conveniently accessed from the C# source code.

Methods are defined inside classes and contain statements. When a method is called, its statements are executed in the same sequence as they are written in the source code.

The equals sign is used in two different fashions, as an assignment operator (=) and as an equality operator (==).

The if statement is able to change the program’s flow of execution. The path followed depends on the Boolean value of its condition.

To make the C# source code clear for the reader, follow a certain format and style.

The .NET Framework class library has comprehensive documentation attached.

Three common statements found in C# are declaration statements, assignment statements, and method call statements.

88 C# PRIMER PLUS

Review Questions

1.How does abstraction help the programmer to cope with complexity?

2.Is the idea behind encapsulation confined to software design? Give an example from everyday life.

3.What are the advantages of using encapsulation in software construction?

4.Which two C# keywords are important for implementing encapsulation?

5.What are the differences between a class and its objects?

6.What is the significance of the class interface?

7.How do you specify the beginning of a comment?

8.Why use comments if the compiler ignores them?

9.What are keywords and identifiers?

10.How is a block specified in C#? What are blocks used for?

11.Can you write a program without a Main method? Why or why not?

12.What are the essential parts of a variable?

13.What is a simple C# instruction called? How is it terminated?

14.Which class and which method can you call in the .NET Framework class library to print text on the console? Write a statement that prints “My dog is brown.”

15.How is a method called? What happens when a method is called?

16.What is an assignment? Which symbol is used to perform an assignment?

17.What is the advantage of declaring variables?

18.How can you make the program decide between two paths of execution?

19.What is whitespace? Does the compiler care much about whitespace?

20.Do all programmers have to follow the same style to write valid C# programs?

Programming Exercises

In the following exercises, you are meant to change and add parts to the program in Listing 3.1 to make it perform the suggested actions.

1.Instead of printing “Bye Bye!” as the last text on the command console before the program finishes, change the source code so that the program writes “Bye Bye. Have a good day!”

Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 89

2.Instead of typing a y to have the program print “Hello World!”, have the user type Yes. Have the program inform the user about this by changing line 10.

3.Instead of using the variable name answer to hold the input from the user, change the name to userInput.

4.Let the program print out an additional line under “Bye Bye. Have a good day!” saying “The program is terminating.”

5.Declare another variable of type string called userName. Before the program prints “Do you want me to write the famous words?”, have the program request the user to type in his or her name. After the user has entered his or her name, have the program read this name and store it in the userName variable. Then have the program print “Hello” followed by the content of the username. Tip: the last printout can be accomplished by typing

System.Console.WriteLine(“Hello” + userName);

A typical execution of the program should result in the following output:

Please type your name

Deborah<enter>

Hello Deborah

Do you want me to write the famous words?

Type Yes for yes; n for no. Then <enter>.

Yes<enter>

Hello World!

Bye Bye. Have a good day!

The program is terminating.