goto
can also be used to jump to a
case
or
default
statement within a
switch
.
Technically, the
case
and
default
statements of a
switch
are labels. Thus, they can be targets
of a
goto
. However, the
goto
statement must be executed from within the
switch
. That is,
you cannot use the
goto
to jump into a
switch
statement. Here is an example that illustrates
goto
with a
switch
:
// Use goto with a switch.
using System;
class SwitchGoto {
static void Main() {
for(int i=1; i < 5; i++) {
switch(i) {
case 1:
Console.WriteLine("In case 1");
goto case 3;
case 2:
Console.WriteLine("In case 2");
goto case 1;
case 3:
Console.WriteLine("In case 3");
goto default;
default:
Console.WriteLine("In default");
break;
}
Console.WriteLine();
}
// goto case 1; // Error! Can't jump into a switch.
}
}
The output from the program is shown here:
In case 1
In case 3
In default
In case 2
In case 1
In case 3
In default
In case 3
In default
In default
www.freepdf-books.com
PART I
C h a p t e r 5 :
P r o g r a m C o n t r o l S t a t e m e n t s
107
PART IPART I
Inside the
switch
, notice how the
goto
is used to jump to other
case
statements or the
default
statement. Furthermore, notice that the
case
statements do not end with a
break
.
Since the
goto
prevents one
case
from falling through to the next, the no fall-through rule
is not violated, and there is no need for a
break
statement. As explained, it is not possible
to use the
goto
to jump into a
switch
. If you remove the comment symbols from the start
of this line
// goto case 1; // Error! Can't jump into a switch.
the program will not compile. Frankly, using a
goto
with a
switch
can be useful in some
special-case situations, but it is not recommended style in general.
One good use for the
goto
is to exit from a deeply nested routine. Here is a simple
example:
// Demonstrate the goto.
using System;
class Use_goto {
static void Main() {
int i=0, j=0, k=0;
for(i=0; i < 10; i++) {
for(j=0; j < 10; j++ ) {
for(k=0; k < 10; k++) {
Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}
stop:
Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
}
}
The output from the program is shown here:
i, j, k: 0 0 0
i, j, k: 0 0 1
i, j, k: 0 0 2
i, j, k: 0 0 3
Stopped! i, j, k: 0, 0 3
Eliminating the
goto
would force the use of three
if
and
break
statements. In this case, the
goto
simplifies the code. While this is a contrived example used for illustration, you can
probably imagine situations in which a
goto
might be beneficial.
One last point: Although you can jump out of a block (as the preceding example shows),
you can’t use the
goto
to jump into a block.
www.freepdf-books.com
This page intentionally left blank
www.freepdf-books.com
6
Introducing Classes and
Objects
T
his chapter introduces the class. The class is the foundation of C# because it defines
the nature of an object. Furthermore, the class forms the basis for object-oriented
programming. Within a class are defined both code and data. Because classes and
objects are fundamental to C#, they constitute a large topic, which spans several chapters.
This chapter begins the discussion by covering their main features.
Class Fundamentals
We have been using classes since the start of this book. Of course, only extremely simple
classes have been used, and we have not taken advantage of the majority of their features.
Classes are substantially more powerful than the limited ones presented so far.
Let’s begin by reviewing the basics. A
class
is a template that defines the form of an
object. It specifies both the data and the code that will operate on that data. C# uses a class
specification to construct
objects.
Objects are
instances
of a class. Thus, a class is essentially a
set of plans that specify how to build an object. It is important to be clear on one issue: A
class is a logical abstraction. It is not until an object of that class has been created that a
physical representation of that class exists in memory.
The General Form of a Class
When you define a class, you declare the data that it contains and the code that operates on
it. While very simple classes might contain only code or only data, most real-world classes
contain both.
In general terms, data is contained in
data members
defined by the class, and code is
contained in
function members.
It is important to state at the outset that C# defines several
specific flavors of data and function members. For example, data members (also called
fields
) include instance variables and static variables. Function members include methods,
constructors, destructors, indexers, events, operators, and properties. For now, we will limit
our discussion of the class to its essential elements: instance variables and methods. Later in
this chapter constructors and destructors are discussed. The other types of members are
described in later chapters.
109
CHAPTER
www.freepdf-books.com
110
P a r t I :
T h e C # L a n g u a g e
A class is created by use of the keyword
class
. Here is the general form of a simple
class
definition that contains only instance variables and methods:
class
classname
{
// declare instance variables
access type var1
;
access type var2
;
// ...
access type varN
;
// declare methods
access ret-type method1
(
parameters
) {
// body of method
}
access ret-type method2
(
parameters
) {
// body of method
}
// ...
access ret-type methodN
(
parameters
) {
// body of method
}
}
Notice that each variable and method declaration is preceded with
access.
Here,
access
is an access specifier, such as
public
, which specifies how the member can be accessed. As
mentioned in Chapter 2, class members can be private to a class or more accessible. The
access specifier determines what type of access is allowed. The access specifier is optional,
and if absent, then the member is private to the class. Members with private access can be
used only by other members of their class. For the examples in this chapter, all members
(except for the
Main( )
method) will be specified as
public
, which means that they can be
used by all other code—even code defined outside the class. We will return to the topic of
access specifiers in Chapter 8.
N
OTE
N
OTE
In addition to an access specifier, the declaration of a class member can also contain one or
more type modifiers. These modifiers are discussed later in this book.
Although there is no syntactic rule that enforces it, a well-designed class should define
one and only one logical entity. For example, a class that stores names and telephone
numbers will not normally also store information about the stock market, average rainfall,
sunspot cycles, or other unrelated information. The point here is that a well-designed class
groups logically connected information. Putting unrelated information into the same class
will quickly destructure your code.
Up to this point, the classes that we have been using have had only one method:
Main( )
.
However, notice that the general form of a class does not specify a
Main( )
method. A
Main( )
method is required only if that class is the starting point for your program.
Define a Class
To illustrate classes, we will be evolving a class that encapsulates information about buildings,
such as houses, stores, offices, and so on. This class is called
Building
, and it will store three
www.freepdf-books.com
Do'stlaringiz bilan baham: |