AWP Notes 2
AWP Notes 2
Programming
Reverses a one-dimensional array so that its elements are backward, from
last to first.
• You can use all the comparison operators with any numeric types.
With string data types, you can use only the equality operators (==
and !=). C# doesn’t support other types of string comparison
operators.
int result;
result = String.Compare("apple", "attach"); // result = -1
result = String.Compare("apple", "all"); // result = 1
result = String.Compare("apple", "apple"); // result = 0
// Another way to perform string comparisons.
string word = "apple";
result = word.CompareTo("attach"); // result = -1
34
34
2.8.1 Conditional Statements The C# Language
The if Statement
• The if statement is the if (myNumber > 10)
powerhouse of conditional {
logic, able to evaluate any
combination of conditions // Do something.
and deal with multiple and }
different pieces of data. else if (myString == "hello")
• Here’s an example with an if {
statement that features two
else conditions: // Do something.
• An if block can have any }
number of conditions. If you else
test only a single condition, {
you don’t need to include any
else blocks. // Do something.
}
break;
default:
// Do something.
break;
}
2.8 LOOPS IN C#
Introduction
• Loops allow you to repeat a segment of code multiple times. C# has
three basic types of loops. You choose the type of loop based on the
type of task you need to perform. Your choices are as follows:
36
36
• You can loop a set number of times with a for loop. The C# Language
• You can loop through all the items in a collection of data by using a
foreach loop.
• You can loop while a certain condition holds true with a while or
do...while loop.
• The for and foreach loops are ideal for chewing through sets of data
that have known, fixed sizes.
value, and the amount to increment with each pass. Here’s one
example:
• You’ll notice that the for loop starts with parentheses that indicate
three important pieces of information. The first portion (int i = 0)
creates the counter variable (i) and sets its initial value (0).
• That means i will be equal to 0 for the first pass, equal to 1 for the
second pass, and so on. However, you could adjust this statement so
that it decrements the counter (or performs any other operation you
want).
37
Advanced Web • The middle portion (i < 10) specifies the condition that must be met
Programming
for the loop to continue. This condition is tested at the start of every
pass through the block. If i is greater than or equal to 10, the
condition will evaluate to false, and the loop will end.
2.8.2 The foreach Loop
• C# also provides a foreach loop that allows you to loop through the
items in a set of data.
• Instead, you create a variable that represents the type of data for
which you’re looking. Your code will then loop until you’ve had a
chance to process each piece of data in the set.
• For example, the next code segment loops through the items in an
array by using foreach.
• In this case, the foreach loop examines each item in the array and
tries to convert it to a string. Thus, the foreach loop defines a string
variable named element.
• For example, if you wanted to loop through an array and change the
values in that array at the same time, foreach code wouldn’t work.
38
38
The C# Language
int i = 0;
while (i < 10)
{
i += 1;
// This code executes ten times. }
• You can also place the condition at the end of the loop by using the
do...while syntax. In this case, the condition is tested at the end of
each pass through the loop:
int i = 0;
do
{
i += 1;
// This code executes ten times.
}
while (i < 10);
39
Advanced Web
Programming
2.9 METHODS IN C#
• Methods are the most basic building block you can use to organize
your code.
• By breaking down your code into methods, you not only simplify
your life, but also make it easier to organize your code into classes
and step into the world of object-oriented programming.
• When you declare a method in C#, the first part of the declaration
specifies the data type of the return value, and the second part
indicates the method name.
• If your method doesn’t return any information, you should use the
void keyword instead of a data type at the beginning of the
declaration.
40
40
The C# Language
private void MyMethodNoReturnedData()
{
// Code goes here.
}
• Private methods are hidden from view and are available only locally,
whereas public methods can be called by all the other classes in your
application.
• To really understand what this means, you’ll need to read the next
chapter, which discusses accessibility in more detail.
• If your method returns data, you have the option of using the data it
returns or just ignoring it:
2.9.1 Parameters
• Methods can also accept information through parameters.
Parameters are declared in a similar way to variables.
41
Advanced Web • Here’s how you might create a function that accepts two parameters
Programming
and returns their sum:
• Now you can look up product prices based on the unique product ID
or the full product name, depending on whether you supply an
integer or string argument:
decimal price;
// Get price by product ID (the first version).
price = GetProductPrice(1001);
// Get price by product name (the second version).
price = GetProductPrice("DVD Player");
• You cannot overload a method with versions that have the same
signature that is, the same number of parameters and parameter data
types because the CLR will not be able to distinguish them from
each other.
• When you call an overloaded method, the version that matches the
parameter list you supply is used. If no version matches, an error
occurs.
Optional and Named Parameters
43