|
|
Wednesday, July 8, 2009
EXCEPTION HANDLING
Wednesday, June 24, 2009
FLOW CONTROL
Conditional statements allow you to
branch your code depending on whether certain conditions are met or on the
value of an expression. C# has two constructs for branching code — the if
statement, which allows you to test whether a specific condition is met, and the
switch statement, which allows you to compare an expression with a number of
different values.
For conditional branching, C#
inherits the C and C++ if...else construct. The syntax should be fairly
intuitive for anyone who has done any programming with a procedural language:
if
(condition)
statement(s)
else
statement(s)
If more than one statement is to be
executed as part of either condition, these statements will need to be joined
together into a block using curly braces ({ ... }). (This also applies to other
C# constructs where statements can be joined into a block, such as the for and
while loops):
bool isZero;
if (i == 0)
{
isZero = true;
Console.WriteLine("i is Zero");
}
else
{
isZero = false;
Console.WriteLine("i is Non-zero");
}
The syntax here is similar to C++
and Java but once again different from Visual Basic. Visual Basic developers
should note that C# does not have any statement corresponding to Visual Basic's
EndIf. Instead, the rule is that each clause of an if contains just one
statement. If you need more than one statement, as in the preceding example,
you should enclose the statements in braces, which will cause the whole group
of statements to be treated as a single block statement.
If you want to, you can use an if
statement without a final else statement. You can also combine else if clauses
to test for multiple conditions:
using
System;
class MainEntryPoint
{
static void Main(string[] args)
{
Console.WriteLine("Type in a string");
string input;
input = Console.ReadLine();
if (input == "")
{
Console.WriteLine("You typed in an empty string");
}
else if (input.Length < 5)
{
Console.WriteLine("The string had less than 5
characters");
}
else if (input.Length < 10)
{
Console.WriteLine("The string had at least 5 but less
than 10
characters");
}
Console.WriteLine("The string was " + input);
}
}
There is no limit to how many else
if's you can add to an if clause.
You'll notice that the previous
example declares a string variable called input, gets the user to enter text at
the command line, feeds this into input, and then tests the length of this
string variable. The code also shows how easy string manipulation can be in C#.
To find the length of input, for example, use input.Length.
One point to note about if is that
you don't need to use the braces if there's only one statement in the
conditional branch:
if (i == 0)
Console.WriteLine("i is Zero"); // This will only execute if i == 0
Console.WriteLine("i can be anything"); // Will execute whatever the
// value of i
However, for consistency, many
programmers prefer to use curly braces whenever they use an if statement.
The if statements presented also
illustrate some of the C# operators that compare values. Note in particular
that, like C++ and Java, C# uses == to compare variables for equality. Do not
use = for this purpose. A single = is used to assign values.
In C#, the expression in the if
clause must evaluate to a Boolean. C++ programmers should be particularly aware
of this; unlike C++, it is not possible to test an integer (returned from a
function, say) directly. In C#, you have to convert the integer that is
returned to a Boolean true or false, for example by comparing the value with
zero or with null:
if (DoSomething() != 0)
{
// Non-zero value returned
}
else
{
// Returned zero
}
This restriction is there in order
to prevent some common types of runtime bugs that occur in C++. In particular,
in C++ it was common to mistype = when == was intended, resulting in
unintentional assignments. In C# this will normally result in a compile-time
error, because unless you are working with bool values, = will not return a
bool.
The switch...case statement is good
for selecting one branch of execution from a set of mutually exclusive ones. It
will be familiar to C++ and Java programmers and is similar to the Select Case
statement in Visual Basic.
It takes the form of a switch
argument followed by a series of case clauses. When the expression in the
switch argument evaluates to one of the values beside a case clause, the code
immediately following the case clause executes. This is one example where you
don't need to use curly braces to join statements into blocks; instead, you
mark the end of the code for each case using the break statement. You can also
include a default case in the switch statement, which will execute if the
expression evaluates to none of the other cases. The following switch statement
tests the value of the integerA variable:
switch (integerA)
{
case 1:
Console.WriteLine("integerA =1");
break;
case 2:
Console.WriteLine("integerA =2");
break;
case 3:
Console.WriteLine("integerA =3");
break;
default:
Console.WriteLine("integerA is not 1,2, or 3");
break;
}
Note that the case values must be
constant expressions; variables are not permitted.
Though the switch...case statement
should be familiar to C and C++ programmers, C#'s switch...case is a bit safer
than its C++ equivalent. Specifically, it prohibits fall-through conditions in
almost all cases. This means that if a case clause is fired early on in the
block, later clauses cannot be fired unless you use a goto statement to mark
that you want them fired too. The compiler enforces this restriction by
flagging every case clause that is not equipped with a break statement as an
error similar to this:
Although it is true that
fall-through behavior is desirable in a limited number of situations, in the
vast majority of cases it is unintended and results in a logical error that's
hard to spot. Isn't it better to code for the norm rather than for the
exception?
By getting creative with goto
statements (which C# does support) however, you can duplicate fall-through
functionality in your switch...cases. However, if you find yourself really
wanting to, you probably should reconsider your approach. The following code
illustrates both how to use goto to simulate fall- through, and how messy the
resultant code can get:
// assume country and language are of type string
switch(country)
{
case "America":
CallAmericanOnlyMethod();
goto case "Britain";
case "France":
language = "French";
break;
case "Britain":
language = "English";
break;
}
There is one exception to the
no–fall-through rule, however, in that you can fall through from one case to
the next if that case is empty. This allows you to treat two or more cases in
an identical way (without the need for goto statements):
switch(country)
{
case "au":
case "uk":
case "us":
language = "English";
break;
case "at":
case "de":
language = "German";
break;
}
One intriguing point about the
switch statement in C# is that the order of the cases doesn't matter — you can
even put the default case first! As a result, no two cases can be the same.
This includes different constants that have the same value, so you can't, for
example, do this:
// assume country is of type string
const string england = "uk";
const string britain = "uk";
switch(country)
{
case england:
case britain: //
this will cause a compilation error
language = "English";
break;
The previous code also shows another
way in which the switch statement is different in C# from C++: In C#, you are
allowed to use a string as the variable being tested.
C# provides four different loops
(for, while, do...while, and foreach) that allow you to execute a block of code
repeatedly until a certain condition is met. The for, while, and do...while
loops are essentially identical to those encountered in C++.
C# for loops provide a mechanism for
iterating through a loop where you test whether a particular condition holds
before you perform another iteration. The syntax is
for
(initializer; condition; iterator)
statement(s)
where
- The initializer is the expression evaluated before the
first loop is executed (usually initializing a local variable as a loop
counter).
- The condition is the expression checked before each new
iteration of the loop (this must evaluate to true for another iteration to
be performed).
- The iterator is an expression evaluated after each
iteration (usually incrementing the loop counter). The iterations end when
the condition evaluates to false.
The for loop is a so-called pre-test
loop, because the loop condition is evaluated before the loop statements are
executed, and so the contents of the loop won't be executed at all if the loop
condition is false.
The for loop is excellent for
repeating a statement or a block of statements for a predetermined number of
times. The following example is typical of the use of a for loop. The following
code will write out all the integers from 0 to 99:
for (int i = 0; i < 100; i = i+1) // this is equivalent to
// For i = 0 To 99 in VB.
{
Console.WriteLine(i);
}
Here, you declare an int called i
and initialize it to zero. This will be used as the loop counter. You then
immediately test whether it is less than 100. Because this condition evaluates
to true, you execute the code in the loop, displaying the value 0. You then
increment the counter by one, and walk through the process again. Looping ends
when i reaches 100.
Actually, the way the preceding loop
is written isn't quite how you would normally write it. C# has a shorthand for
adding 1 to a variable, so instead of i=i+1, you can simply write i++:
for (int i = 0; i < 100; i++)
{
C# for loop syntax is far more
powerful than the Visual Basic For...Next loop, because the iterator can be any
statement. In Visual Basic, all you can do is add or subtract some number from
the loop control variable. In C# you can do anything; for example, you can
multiply the loop control variable by 2.
It's not unusual to nest for loops
so that an inner loop executes once completely for each iteration of an outer
loop. This scheme is typically employed to loop through every element in a
rectangular multidimensional array. The outermost loop loops through every row,
and the inner loop loops through every column in a particular row. The
following code displays rows of numbers. It also uses another Console method,
Console.Write(), which does the same as Console.WriteLine() but doesn't send a
carriage return to the output.
using
System;
class MainEntryPoint
{
static void Main(string[] args)
{
// This loop iterates through rows...
for (int i = 0; i < 100; i+=10)
{
// This loop iterates through columns...
for (int j = i; j < i + 10; j++)
{
Console.Write("
" + j);
}
Console.WriteLine();
}
}
}
Although j is an integer, it will be
automatically converted to a string so that the concatenation can take place.
C++ developers will note that this is far easier than string handling ever was
in C++; for Visual Basic developers this is familiar ground.
C programmers should take note of
one particular feature of the preceding example. The counter variable in the
innermost loop is effectively re-declared with each successive iteration of the
outer loop. This syntax is legal not only in C# but in C++ as well.
The preceding sample results in this
output:
0 1 2
3 4
5 6 7
8 9
10 11
12 13 14
15 16 17
18 19
20 21
22 23 24
25 26 27
28 29
30 31
32 33 34
35 36 37
38 39
40 41
42 43 44
45 46 47
48 49
50 51
52 53 54
55 56 57
58 59
60 61
62 63 64
65 66 67
68 69
70 71
72 73 74
75 76 77
78 79
80 81
82 83 84
85 86 87
88 89
90 91 92 93
94 95 96
97 98 99
Although it is technically possible
to evaluate something other than a counter variable in a for loop's test
condition, it is certainly not typical. It is also possible to omit one (or
even all) of the expressions in the for loop. In such situations, however, you
should consider using the while loop.
The while loop is identical to the
while loop in C++ and Java, and the While...Wend loop in Visual Basic. Like the
for loop, while is a pre-test loop. The syntax is similar, but while loops take
only one expression:
while(condition)
statement(s);
Unlike the for loop, the while loop
is most often used to repeat a statement or a block of statements for a number
of times that is not known before the loop begins. Usually, a statement inside
the while loop's body will set a Boolean flag to false on a certain iteration,
triggering the end of the loop, as in the following example:
bool condition = false;
while (!condition)
{
// This loop spins until the condition is true
DoSomeWork();
condition = CheckCondition(); // assume CheckCondition() returns a bool
}
All of C#'s looping mechanisms,
including the while loop, can forego the curly braces that follow them if they
intend to repeat just a single statement and not a block of statements. Again,
many programmers consider it good practice to use braces all of the time.
The do...while loop is the post-test
version of the while loop. It does the same thing with the same syntax as
do...while in C++ and Java, and the same thing as Loop...While in Visual Basic.
This means that the loop's test condition is evaluated after the body of the
loop has been executed. Consequently,do...while loops are useful for situations
in which a block of statements must be executed at least one time, as in this
example:
bool condition;
do
{
// this loop will at least execute once, even if Condition
is false
MustBeCalledAtLeastOnce();
condition = CheckCondition();
} while (condition);
The foreach loop is the final C#
looping mechanism that we discuss. Whereas the other looping mechanisms were
present in the earliest versions of C and C++, the foreach statement is a new
addition (bor- rowed from Visual Basic), and a very welcome one at that.
The foreach loop allows you to
iterate through each item in a collection.
Technically, to count as a collection, it must support an interface
called IEnumerable. Examples of collections include C# arrays, the collection
classes in the System.Collection namespaces, and user-defined collection
classes. You can get an idea of the syntax of foreach from the following code,
if you assume that arrayOfInts is (unsurprisingly) an array if ints:
foreach (int temp in arrayOfInts)
{
Console.WriteLine(temp);
}
Here, foreach steps through the
array one element at a time. With each element, it places the value of the
element in the int variable called temp, and then performs an iteration of the
loop.
An important point to note with
foreach is that you can't change the value of the item in the collection (temp
in the preceding code), so code such as the following will not compile:
foreach (int temp in arrayOfInts)
{
temp++;
Console.WriteLine(temp);
}
If you need to iterate through the
items in a collection and change their values, you will need to use a for loop
instead.
C# provides a number of statements
that allow you to jump immediately to another line in the program. The first of
these is, of course, the notorious goto statement.
The goto statement allows you to
jump directly to another specified line in the program, indicated by a label
(this is just an identifier followed by a colon):
goto Label1;
Console.WriteLine("This won't be
Label1:
Console.WriteLine("Continuing execution from
here");
A couple of restrictions are
involved with goto. You can't jump into a block of code such as a for loop, you
can't jump out of a class, and you can't exit a finally block after try...catch
blocks.
The reputation of the goto statement
probably precedes it, and in most circumstances, its use is sternly frowned
upon. In general, it certainly doesn't conform to good object-oriented
programming practice. However, there is one place where it is quite handy:
jumping between cases in a switch statement, particularly because C#'s switch
is so strict on fall-through. You saw the syntax for this earlier in this
chapter.
You have already met the break
statement briefly — when you used it to exit from a case in a switch statement.
In fact, break can also be used to exit from for, foreach, while, or do...while
loops too. Control will switch to the statement immediately after the end of
the loop.
If the statement occurs in a nested
loop, control will switch to the end of the innermost loop. If the break occurs
outside of a switch statement or a loop, a compile-time error will occur.
The continue statement is similar to
break, and must also be used within a for, foreach, while, or do...while loop.
However, it exits only from the current iteration of the loop, meaning
execution will restart at the beginning of the next iteration of the loop,
rather than outside the loop altogether.
The return statement is used to exit
a method of a class, returning control to the caller of the method. If the
method has a return type, return must return a value of this type; otherwise if
the method returns void, you should use return without an expression.
Tuesday, June 23, 2009
CLASSES
Defining
Classes
Classes
are the most fundamental of C#’s types. A class is a data structure that
combines state (fields) and actions (methods and other function members) in a
single unit. A class provides a definition for dynamically created instances of the class, also known as objects.
Classes support inheritance and polymorphism,
mechanisms whereby derived classes can extend and
specialize base classes.
New classes are created
using class declarations. A class declaration starts with a header that
specifies the attributes and modifiers of the class, the name of the class, the
base class (if any), and the interfaces implemented by the class. The header is
followed by the class body, which consists of a list of member declarations
written between the delimiters {
and }.
The syntax used for defining classes
in C# is simple, especially if you usually program in C++ or Java:
[attributes] [modifiers] class <className> [: baseClassName]
{
[class-body]
}[;]
let’s look at a simple class declaration to
get started:
class Employee
{
private long employeeId;
}
As you can see, this class is as
basic as it gets. We have a class named Employee that contains a single member
named employeeId. Notice private, which precedes our member. This is called an access
modifier.
If you come from a C++ background,
note that it’s not necessary to place a terminating semicolon at the end of a
class. However, you can do so if you want; in fact, I do this in some of my
classes simply out of habit. Having said that, the following two class
declarations are identical:
class MyClass
{
// Members
}
class MyClass
{
// Members
};
Now let’s look at the various types
of members that can be defined with a class.
Class
Members
Put simply, classes consist of
members. Everything you define within a class is considered to be a member of
that class. You already know from Chapter 2, “The .NET Type System,” that all
the types available to C# programmers originate from the common type system
(CTS). Here’s a list of the various types that you can define as members of a
C# class:
- Fields
A field is
a member variable used to hold a value. You can apply several modifiers to a
field, depending on how you want the field used. These modifiers include
static, readonly, and const. We’ll discuss what these modifiers signify and how
to use them shortly.
- Methods
A method
is the actual code that acts on the object’s data (or field values).
- Properties
Properties
are sometimes called smart fields because they’re actually methods that look
like fields to the class’s clients. This allows the client a greater degree of
abstraction because it doesn’t have to know whether it’s accessing the field
directly or whether an accessor method is being called.
- Constants
As the
name suggests, a constant is a field with a value that can’t be changed .
- Indexers
Just as a
property is a smart field, an indexer is sometimes referred to as a smart
array—that is, a member that enables you to work with a class that’s logically
an array of data, as though the class itself were an array. For example,
although a ListBox class might have other fields (such as a sort bit) and
methods to manipulate and present the control’s data, this class will be
conceptually defined by the array of data elements that it’s displaying.
However, to index the class’s data, the class designer typically either allows
direct access to the class’s internal data array or provides a method that
takes a subscript, or index, value to define which array element is being dealt
with. Indexers, enable you to subscript
the actual object, thereby giving the client a much more intuitive means of
using the class.
- Events
An event
causes some piece of code to run. Events are an integral part of Microsoft
Windows programming. For example, an event is fired when a user moves the mouse
or clicks or resizes a window. C# events use the standard publish/subscribe
design pattern seen in Microsoft Message Queuing (MSMQ) and the COM+
asynchronous event model, which gives an application asynchronous event-handling
capabilities. But in C#, this design pattern is a “first-class” concept—meaning
that the language was designed with this in mind instead of being bolted on
after the fact.
- Operators
C# gives
you the ability, via operator overloading, to add the standard mathematical
operators to a class so that you can write more intuitive code.
Access
Modifiers
Now that you’ve briefly read about
the different types that can be defined as members of a C# class, let’s look at
some important modifiers used to specify how visible, or accessible, a given
member is to code outside its own class. These modifiers, called access
modifiers, are listed along with their descriptions in the following table
C# Access Modifiers
|
|
Access Modifier
|
Description
|
Public
|
Signifies that the member is
accessible from outside the class’s definition and hierarchy of derived
classes.
|
Protected
|
The member isn’t visible outside
the class and can be accessed by derived classes only.
|
Private
|
The member can’t be accessed
outside the scope of the defining class. Therefore, not even derived classes
have access to these members.
|
Internal
|
The member is visible only within
the current compilation unit. The internal access modifier creates a hybrid
of public and protected accessibility, depending on where the code resides.
|
Note that unless you want a member
to have the default access modifier of private, you must specify an access
modifier for it. This is in contrast to C++, in which a member that’s not
explicitly decorated with an access modifier assumes the visibility
characteristics of the previously stated access modifier. For example, in the
following C++ code, the members a, b, and c are defined with public visibility,
and the members d and e are defined as protected members:
class CAccessModsInCpp
{
public:
int a;
int b;
int c;
protected:
int d;
int e;
};
To accomplish the same goal in C#,
you would have to change this code to the following:
class AccessModsInCSharp
{
public int a;
public int b;
public int c;
protected int d;
protected int e;
}
The following C# code results in the
member b being declared as private because no access modifier was specified for
that member:
public MoreAccessModsInCSharp
{
public int a;
int b;
}
The
Main Method
Every C# application must define a
method named Main in one of its classes. This method, which serves as that
application’s entry point, must be defined as static. (We’ll discuss what
static means shortly.) It doesn’t matter to the C# compiler which class has the
Main method defined, and the class you choose doesn’t affect the order of
compilation. This is unlike C++, in which you must monitor dependencies closely
when building an application. The C# compiler is smart enough to go through
your source files and locate the Main method on its own. However, this
all-important method is the entry point to all C# applications.
Although you can place the Main
method in any class, I recommend creating a class specifically for housing this
method. Here’s an example of doing that using our—so far—simple Employee class:
class Employee
{
private int employeeId;
}
class AppClass
{
static public void Main()
{
Employee emp = new Employee();
}
}
As you can see, the example has two
classes. This is a common approach to C# programming even in the simplest
applications. The first class (Employee) is a problem domain class, and the
second class (AppClass) contains the needed entry point (Main) for the
application. In this case, the Main method instantiates the Employee object. If
this were a real application, the Main method would use the Employee object’s
members.
Command-Line
Arguments
You can access the command-line
arguments to an application by declaring the Main method as taking a string array
type as its only argument. Then you can process the arguments as you would any
array.Here’s some generic code for iterating through the command-line arguments
of an application and writing them to the standard output device:
using System;
class CommandLine
{
public static void Main(string[] args)
{
Console.WriteLine("\nYou specified {0} arguments",
args.Length);
foreach (string arg in args)
{
Console.WriteLine("Argument: {0}", arg);
}
}
}
Figure….. shows an example of
calling this application with a couple of randomly selected values.
Returning
Values from Main
Most examples in this book define
Main as follows:
class SomeClass
{
// Other members
public static void Main()
{
// Main body
}
}
However, you can also define Main to
return a value of type int. Although not common in GUI applications, this
return value can be useful when you’re writing console applications to be run
in batch. The return statement terminates execution of the method, and the
returned value is used as an error level to the calling application or batch
file to indicate user-defined success or failure. To do this, use the following
prototype:
public static int Main()
{
// Return some value of type int
// that represents success or value.
return 0;
}
Multiple
Main Methods
The designers of C# included a
mechanism by which you can define more than one class with a Main method. Why
would you want to do this? One reason is to place test code in your classes.
You can then use the /main:<className> switch with the C# compiler to
specify which class’s Main method to use. Here’s an example that has two
classes containing Main methods:
using System;
class Main1
{
public static void Main()
{
Console.WriteLine("Main1");
}
}
class Main2
{
public static void Main()
{
Console.WriteLine("Main2");
}
}
To compile this application so that
the Main1.Main method is used as the application’s entry point, you’d use this
switch:
csc MultipleMain.cs /main:Main1
Subscribe to:
Posts (Atom)