PDA

View Full Version : كتب c# باللغة الانجليزية



zied toumi
10-19-2013, 23:22
The quest for the perfect programming language is as old as the discipline of programming itself.
In this quest, C# is the current standard bearer. Created by Microsoft to support development
for its .NET Framework, C# leverages time-tested features with cutting-edge innovations and
provides a highly usable, efficient way to write programs for the modern enterprise computing
environment. In the course of this book, you will learn to program using it.
The purpose of this chapter is to introduce C#, including the forces that drove its creation,
its design philosophy, and several of its most important features. By far, the hardest thing
about learning a programming language is the fact that no element exists in isolation. Instead,
the components of the language work together. It is this interrelatedness that makes it difficult
to discuss one aspect of C# without involving others. To overcome this problem, this chapter
provides a brief overview of several C# features, including the general form of a C# program,
two control statements, and several operators. It does not go into too many details, but rather
concentrates on the general concepts common to any C# program.
At this time, version 3.0 is the current release of C#, and this is the version taught in this
book. Of course, much of the information presented here applies to all versions of C#.
C#’s Family Tree
Computer languages do not exist in a void. Rather, they relate to one another, with each new
language influenced in one form or another by the ones that came before. In a process akin
to cross-pollination, features from one language are adapted by another, a new innovation is
integrated into an existing context, or an older construct is removed. In this way, languages
evolve and the art of programming advances. C# is no exception.

---------- Post added at 08:21 PM ---------- Previous post was at 08:20 PM ----------

C# inherits a rich programming legacy. It is directly descended from two of the world’s
most successful computer languages: C and C++. It is closely related to another: Java.
Understanding the nature of these relationships is critical to understanding C#. Thus, we begin
our examination of C# by placing it in the historical context of these three languages.
C: The Beginning of the Modern Age of Programming
The creation of C marks the beginning of the modern age of programming. C was invented by
Dennis Ritchie in the 1970s on a DEC PDP-11 that used the UNIX operating system. While
some earlier languages, most notably Pascal, had achieved significant success, it was C that
established the paradigm that still charts the course of programming today.
C grew out of the structured programming revolution of the 1960s. Structured languages are
defined by their rich set of well-designed control statements, subroutines with local variables,
code blocks, and other improvements that make it easier to organize and maintain a program.
Although other languages at the time had similar features, C implemented these elements using
a terse yet easy-to-use syntax. It also embodied a philosophy that put the programmer (not the
language) at the center of the development process. As a result, C quickly won many converts.
It became the dominant structured programming language of the late 1970s and 1980s, and it
remains so to this day.
The Creation of OOP and C++
As useful as C is, by the late 1970s, the size of many projects was near or at the limits of what
structured programming methodologies and the C language could handle. Programs were
simply becoming too large. To address this problem, a new way to program began to emerge.
This method is called object-oriented programming (OOP for short). Using OOP, a programmer
could handle much larger programs. The trouble was that C, the most popular language at the
time, did not support object-oriented programming. The desire for an object-oriented version of
C ultimately led to the creation of C++.
C++ was invented by Bjarne Stroustrup, beginning in 1979, at Bell Laboratories in Murray
Hill, New Jersey. He initially called the new language “C with Classes.” However, in 1983, the
name was changed to “C++.” C++ contains the entire C language. Thus, C is the foundation
upon which C++ is built. Most of the additions that Stroustrup made to C were designed to
support object-oriented programming. In essence, C++ is the object-oriented version of C.
By building upon the foundation of C, Stroustrup provided a smooth migration path to
OOP. Instead of having to learn an entirely new language, a C programmer needed to learn only
a few new features before reaping the benefits of the object-oriented methodology. This made
it easy for legions of programmers to make the shift from structured programming to objectoriented
programming. As a result, by the second half of the 1990s, C++ became the preeminent
language for the development of high-performance code.
It is critical to understand that the invention of C++ was not an attempt to create a new
programming language. Instead, it was an enhancement to an already highly successful
language. This approach to language development—beginning with an existing language and
moving it forward—established a trend that continues today.

---------- Post added at 08:22 PM ---------- Previous post was at 08:21 PM ----------

The Internet and Java Emerge
The next major advance in programming languages was Java. Work on Java, which was originally
called Oak, began in 1991 at Sun Microsystems. The main driving force behind Java’s design was
James Gosling. Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan also played a role.
Java is a structured, object-oriented language with a syntax and philosophy derived
from C++. The innovative aspects of Java were driven not so much by advances in the art
of programming (although some certainly were), but rather by changes in the computing
environment. Prior to the mainstreaming of the Internet, most programs were written, compiled,
and targeted for a specific CPU and a specific operating system. While it has always been
true that programmers like to reuse their code, the ability to easily port a program from one
environment to another took a backseat to more pressing problems. However, with the rise of
the Internet, in which many different types of CPUs and operating systems are connected, the
old problem of portability became substantially more important. To solve this problem, a new
language was needed, and this new language was Java.
Java achieved portability by translating a program’s source code into an intermediate
language called bytecode. This bytecode was then executed by the Java Virtual Machine (JVM).
Therefore, a Java program could run in any environment for which a JVM was available. Also,
since the JVM is relatively easy to implement, it was readily available for a large number of
environments.
In addition to the need for portability, there was a second fundamental problem that
needed to be solved before Internet-based programming could become a reality. This problem
was security. As all Internet users know, computer viruses constitute a serious and on-going
potential threat. What good would portable programs be if no one could trust them? Who
would want to risk executing a program delivered via the Internet? It might contain malicious
code. Fortunately, the solution to the security problem is also found in the JVM and bytecode.
Because the JVM executes the bytecode, it has full control of the program and can prevent a
Java program from doing something that it shouldn’t. Thus, the JVM and bytecode solved both
the issues of portability and security.
It is key to understand that Java’s use of bytecode differed radically from both C and C++,
which were nearly always compiled to executable machine code. Machine code is tied to a
specific CPU and operating system. Thus, if you wanted to run a C/C++ program on a different
system, it needed to be recompiled to machine code specifically for that environment. To create
a C/C++ program that would run in a variety of environments, several different executable
versions of the program were needed. Not only was this impractical, it was also expensive.
Java’s use of an intermediate language was an elegant and cost-effective solution. It was also
a solution that C# would adapt for its own purposes.
As mentioned, Java is descended from C and C++. Its syntax is based on C, and its
object model is evolved from C++. Although Java code is neither upwardly nor downwardly
compatible with C or C++, its syntax is sufficiently similar that the large pool of existing C/C++
programmers could move to Java with very little effort. Furthermore, because Java built upon
and improved an existing paradigm, Gosling, et al., were free to focus their attentions on the
new and innovative features. Just as Stroustrup did not need to “reinvent the wheel” when
creating C++, Gosling did not need to create an entirely new language when developing Java.

zied toumi
10-19-2013, 23:25
Moreover, with the creation of Java, C and C++ became an accepted substrata upon which to
build a new computer language.
The Creation of C#
While Java has successfully addressed many of the issues surrounding portability and security
in the Internet environment, there are still features that it lacks. One is cross-language
interoperability, also called mixed-language programming. This is the ability for the code
produced by one language to work easily with the code produced by another. Cross-language
interoperability is crucial for the creation of large, distributed software systems. It is also
desirable for programming software components, because the most valuable component is
one that can be used by the widest variety of computer languages in the greatest number of
operating environments.
Another feature lacking in Java is full integration with the Windows platform. Although
Java programs can be executed in a Windows environment (assuming that the Java Virtual
Machine has been installed), Java and Windows are not closely coupled. Since Windows is
the mostly widely used operating system in the world, lack of direct support for Windows is a
drawback to Java.
To answer these and other needs, Microsoft developed C#. C# was created at Microsoft late
in the 1990s and was part of Microsoft’s overall .NET strategy. It was first released in its alpha
version in the middle of 2000. C#’s chief architect was Anders Hejlsberg.
C# is directly related to C, C++, and Java. This is not by accident. These are three of the
most widely used—and most widely liked—programming languages in the world. Furthermore,
nearly all professional programmers today know C, C++, or Java. By building C# upon a solid,
well-understood foundation, C# offers an easy migration path from these languages. Since it was
neither necessary nor desirable for Hejlsberg to “reinvent the wheel,” he was free to focus on
specific improvements and innovations.
The family tree for C# is shown in Figure 1-1. The *****father of C# is C. From C, C#
derives its syntax, many of its keywords, and operators. C# builds upon and improves the object
model defined by C++. If you know C or C++, you will feel at home with C#.

---------- Post added at 08:23 PM ---------- Previous post was at 08:23 PM ----------

C# and Java have a bit more complicated relationship. As explained, Java is also descended
from C and C++. It, too, shares the C/C++ syntax and object model. Like Java, C# is designed
to produce portable code, and C# programs execute in a secure controlled runtime environment.
However, C# is not descended from Java. Instead, C# and Java are more like cousins, sharing a
common ancestry, but differing in many important ways. The good news, though, is that if you
know Java, many C# concepts will be familiar. Conversely, if in the future you need to learn
Java, many of the things you learn about C# will carry over.
C# contains many innovative features that we will examine at length throughout the course
of this book, but some of its most important features relate to its built-in support for software
components. In fact, C# has been characterized as being a component-oriented language because
it contains integral support for the writing of software components. For example, C# includes
features that directly support constituents of components, such as properties, methods, and
events. However, C#’s ability to work in a secure, mixed-language environment is perhaps its
most important component-oriented feature.
The Evolution of C#
Since its original 1.0 release, C# has been evolving at a rapid pace. Not long after C# 1.0,
Microsoft released version 1.1. It contained many minor tweaks, but added no major features.
However, the situation was much different with the release of C# 2.0. This was a watershed
event in the lifecycle of C# because version 2.0 added many new features, such as generics,
partial types, and anonymous methods, that fundamentally expanded the scope, power,
and range of the language. Version 2.0 firmly put C# at the forefront of computer language
development. It also demonstrated Microsoft’s long-term commitment to the language.
The next major release of C# was 3.0, and (at the time of this writing) this is the current
version of C#. Because of the many new features added by C# 2.0, one might have expected
the development of C# to slow a bit, just to let programmers catch up, but this was not the case.
With version 3.0, Microsoft once again put C# on the cutting edge of language design, this time
adding a set of innovative features that redefined the programming landscape.
Perhaps the two most exciting new features in C# 3.0 are language-integrated query
(LINQ) and lambda expressions. LINQ enables you to write database queries using C#
programming elements. Lambda expressions are often used in LINQ expressions. Together
they add an entirely new dimension to C# programming. Other innovations include implicitly
typed variables and extension methods. Because this book is based on C# 3.0, these important
advances are covered.
How C# Relates to the .NET Framework
Although C# is a computer language that can be studied on its own, it has a special
relationship to its runtime environment, the .NET Framework. The reason for this is twofold.
First, C# was initially designed by Microsoft to create code for the .NET Framework. Second,
the libraries used by C# are the ones defined by the .NET Framework. Thus, even though it is

---------- Post added at 08:24 PM ---------- Previous post was at 08:23 PM ----------

possible to separate the C# language from the .NET environment, the two are closely linked.

---------- Post added at 08:24 PM ---------- Previous post was at 08:24 PM ----------

Because of this, it is important to have a general understanding of the .NET Framework and
why it is important to C#.
What Is the .NET Framework?
In a sentence, the .NET Framework defines an environment that supports the development and
execution of highly distributed, component-based applications. It enables different computer
languages to work together and provides for security, program portability, and a common
programming model for the Windows platform.
As it relates to C#, the .NET Framework defines two very important entities. The first is the
Common Language Runtime. This is the system that manages the execution of your program.
Along with other benefits, the Common Language Runtime is the part of the .NET Framework
that enables programs to be portable, supports mixed-language programming, and provides for
security.
The second entity is the .NET class library. This library gives your program access to the
runtime environment. For example, if you want to perform I/O, such as displaying something
on the screen, you will use the .NET class library to do it. If you are new to programming, the
term class may be new. Although it will be explained in detail a bit later, briefly, a class is an
object-oriented construct that helps organize programs. As long as your program restricts itself
to the features defined by the .NET class library, your programs can run anywhere that the .NET
runtime system is supported. Since C# automatically uses the .NET class library, C# programs
are automatically portable to all .NET environments.

---------- Post added at 08:25 PM ---------- Previous post was at 08:24 PM ----------

How the Common Language Runtime Works
The Common Language Runtime (CLR) manages the execution of .NET code. Here is how
it works. When you compile a C# program, the output of the compiler is not executable code.
Instead, it is a file that contains a special type of pseudocode called Microsoft Intermediate
Language, or MSIL for short. MSIL defines a set of portable instructions that are independent
of any specific CPU. In essence, MSIL defines a portable assembly language. One other point:
Although MSIL is similar in concept to Java’s bytecode, the two are not the same.
It is the job of the CLR to translate the intermediate code into executable code when a
program is run. Thus, any program compiled to MSIL can be run in any environment for which
the CLR is implemented. This is part of how the .NET Framework achieves portability.
Microsoft Intermediate Language is turned into executable code using a JIT compiler. JIT
stands for “just in time.” The process works like this: When a .NET program is executed, the
CLR activates the JIT compiler. The JIT compiler converts MSIL into native code on a demand
basis, as each part of your program is needed. Thus, your C# program actually executes as
native code, even though it was initially compiled into MSIL. This means that your program
runs nearly as fast as it would if it had been compiled to native code in the first place, but it
gains the portability and security benefits of MSIL.
In addition to MSIL, one other thing is output when you compile a C# program: metadata.
Metadata describes the data used by your program and enables your code to interact with other
code. The metadata is contained in the same file as the MSIL.

zied toumi
10-19-2013, 23:29
Fortunately, for the purposes of this book, and for the majority of programming tasks, it
is not necessary to know any more about the CLR, MSIL, or metadata. C# handles the details
for you.
Managed vs. Unmanaged Code
In general, when you write a C# program, you are creating what is called managed code.
Managed code is executed under the control of the Common Language Runtime, as just
described. Because it is running under the control of the CLR, managed code is subject to
certain constraints—and derives several benefits. The constraints are easily described and met:
The compiler must produce an MSIL file targeted for the CLR (which C# does) and use the
.NET Framework library (which C# does). The benefits of managed code are many, including
modern memory management, the ability to mix languages, better security, support for version
control, and a clean way for software components to interact.
The opposite of managed code is unmanaged code. Unmanaged code does not execute
under the Common Language Runtime. Thus, all Windows programs prior to the creation of the
.NET Framework use unmanaged code. It is possible for managed code and unmanaged code to
work together, so the fact that C# generates managed code does not restrict its ability to operate
in conjunction with preexisting programs.

---------- Post added at 08:26 PM ---------- Previous post was at 08:26 PM ----------

The Common Language Specification
Although all managed code gains the benefits provided by the CLR, if your code will be used
by other programs written in different languages, for maximum usability, it should adhere to
the Common Language Specification (CLS). The CLS describes a set of features, such as data
types, that different languages have in common. CLS compliance is especially important when
creating software components that will be used by other languages. Although we won’t need to
worry about the CLS for the purposes of this book, it is something you will want to look into
when you begin writing commercial code.
Q: To address the issues of portability, security, and mixed-language programming, why
was it necessary to create a new computer language such as C#? Couldn’t a language
like C++ be adapted to support the .NET Framework?
A: Yes, it is possible to adapt C++ so that it produces .NET-compatible code that runs under
the CLR. In fact, Microsoft did just that. Initially, Microsoft added what are called the
managed extensions to C++. However, this approach has been rendered obsolete and is
replaced by a set of extended keywords and syntax defined by the Ecma C++/CLI Standard.
(CLI stands for Common Language Infrastructure.) Although C++/CLI make it possible to
port existing code to the .NET Framework, new .NET development is much easier in C#
because it was originally designed with .NET in mind.

---------- Post added at 08:27 PM ---------- Previous post was at 08:26 PM ----------

Object-Oriented Programming
At the center of C# is object-oriented programming (OOP). The object-oriented methodology is
inseparable from C#, and all C# programs are, at least to some extent, object-oriented. Because
of its importance to C#, it is useful to understand OOP’s basic principles before you write even
a simple C# program.
OOP is a powerful way to approach the job of programming. Programming methodologies
have changed dramatically since the invention of the computer, primarily to accommodate
the increasing complexity of programs. For example, when computers were first invented,
programming was done by toggling in the binary machine instructions using the computer’s
front panel. As long as programs were just a few hundred instructions long, this approach
worked. As programs grew, assembly language was invented so that a programmer could deal
with larger, increasingly complex programs, using symbolic representations of the machine
instructions. As programs continued to grow, high-level languages, such as FORTRAN
and COBOL, were introduced that gave the programmer more tools with which to handle
complexity. When these early languages began to reach their breaking point, structured
programming was invented.

---------- Post added at 08:27 PM ---------- Previous post was at 08:27 PM ----------

Consider this: At each milestone in the development of programming, techniques and tools
were created to allow the programmer to deal with increasingly greater complexity. Each step of
the way, the new approach took the best elements of the previous methods and moved forward.
The same is true of object-oriented programming. Prior to OOP, many projects were nearing
(or exceeding) the point where the structured approach no longer worked. A better way to
handle complexity was needed, and object-oriented programming was the solution.
Object-oriented programming took the best ideas of structured programming and combined
them with several new concepts. The result was a different and better way of organizing a
program. In the most general sense, a program can be organized in one of two ways: around
its code (what is happening) or around its data (what is being affected). Using only structured
programming techniques, programs are typically organized around code. This approach can be
thought of as “code acting on data.”
Object-oriented programs work the other way around. They are organized around data, with
the key principle being “data controlling access to code.” In an object-oriented language, you
define the data and the routines that are permitted to act on that data. Thus, a data type defines
precisely what sort of operations can be applied to that data.
To support the principles of object-oriented programming, all OOP languages, including C#,
have three traits in common: encapsulation, polymorphism, and inheritance. Let’s examine each.

---------- Post added at 08:28 PM ---------- Previous post was at 08:27 PM ----------

Encapsulation
Encapsulation is a programming mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse. In an object-oriented language, code
and data can be bound together in such a way that a self-contained black box is created. Within
the box are all necessary data and code. When code and data are linked together in this fashion, an
object is created. In other words, an object is the device that supports encapsulation.
Within an object, code, data, or both may be private to that object or public. Private code or
data is known to and accessible by only another part of the object. That is, private code or data
cannot be accessed by a piece of the program that exists outside the object. When code or data
is public, other parts of your program can access it, even though it is defined within an object.
Typically, the public parts of an object are used to provide a controlled interface to the private
elements of the object.

---------- Post added at 08:29 PM ---------- Previous post was at 08:28 PM ----------

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.
The code and data that constitute a class are called members of the class. Specifically,
the data defined by the class is referred to as member variables or instance variables. The
code that operates on that data is referred to as member methods or just methods. “Method”
is C#’s term for a subroutine. If you are familiar with C/C++, it may help to know that what a
C# programmer calls a method, a C/C++ programmer calls a function. Because C# is a direct
descendent of C++, the term “function” is also sometimes used when referring to a C# method.
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is the quality that allows one interface to
access a general class of actions. A simple example of polymorphism is found in the steering
wheel of an automobile. The steering wheel (the interface) is the same, no matter what type of
actual steering mechanism is used. That is, the steering wheel works the same whether your
car has manual steering, power steering, or rack-and-pinion steering. Thus, turning the steering
wheel left causes the car to go left no matter what type of steering is used. The benefit of the
uniform interface is, of course, that once you know how to operate the steering wheel, you can
drive any type of car

zied toumi
10-19-2013, 23:34
The same principle can also apply to programming. For example, consider a stack (which
is a first-in, last-out list). You might have a program that requires three different types of stacks.
One stack is used for integer values, one for floating-point values, and one for characters. In this
case, the algorithm that implements each stack is the same, even though the data being stored
differs. In a non–object-oriented language, you would be required to create three different sets
of stack routines, with each set using different names. However, because of polymorphism,
in C#, you can specify the general form of a stack once and then use it for all three specific
situations. This way, if you know how to use one stack, you can use them all.
More generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods.” This means that it is possible to design a generic interface to
a group of related activities. Polymorphism helps reduce complexity by allowing the same
interface to be used to specify a general class of action. It is the compiler’s job to select the
specific action (that is, method) as it applies to each situation. You, the programmer, don’t need
to do this selection manually. You need only remember and utilize the general interface.
Inheritance
Inheritance is the process by which one object can acquire the properties of another object. This
is important because it supports the concept of hierarchical classification. If you think about it,
most knowledge is made manageable by hierarchical (that is, top-down) classifications.

---------- Post added at 08:30 PM ---------- Previous post was at 08:29 PM ----------

Q: You state that object-oriented programming is an effective way to manage large
programs. However, it seems that OOP might add substantial overhead to relatively
small programs. Since you say that all C# programs are, to some extent, objectoriented,
does this impose a penalty for smaller programs?
A: No. As you will see, for small programs, C#’s object-oriented features are nearly transparent.
Although it is true that C# follows a strict object model, you have wide latitude as to the
degree to which you employ it. For smaller programs, their “object-orientedness” is barely
perceptible. As your programs grow, you will integrate more object-oriented features
effortlessly.

---------- Post added at 08:31 PM ---------- Previous post was at 08:30 PM ----------

For example, a Red Delicious apple is part of the classification apple, which, in turn, is part
of the fruit class, which is under the larger class food. That is, the food class possesses certain
qualities (edible, nutritious, and so on) that also, logically, apply to its subclass, fruit. In addition
to these qualities, the fruit class has specific characteristics (juicy, sweet, and so forth) that
distinguish it from other food. The apple class defines those qualities specific to an apple (grows
on trees, not tropical, and so on). A Red Delicious apple would, in turn, inherit all the qualities of
all preceding classes and would define only those qualities that make it unique.
Without the use of hierarchies, each object would have to explicitly define all of its
characteristics. Using inheritance, an object need only define those qualities that make it unique
within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance
mechanism that makes it possible for one object to be a specific instance of a more general case.
Creating, Compiling, and Running
Your First C# Program
It is now time to learn how to create, compile, and run a C# program. Since this is a “hands-on”
guide to C# programming, being able to successfully complete these tasks is a necessary first
step. Here is the program we will use:
/*
This is a simple C# program.
Call this program Example.cs.
*/
using System;
class Example {

---------- Post added at 08:31 PM ---------- Previous post was at 08:31 PM ----------

// A C# program begins with a call to Main().
static void Main() {
Console.WriteLine("C# gives you programming power.");
}
}
Although quite short, this program does contain several key features that are common to all C#
programs. Once you have learned how to compile and run it, we will examine it in detail.
Obtain a C# 3.0 Compiler
To create, compile, and run a C# program, you will need a copy of Microsoft’s Visual C#. This
book uses Visual C# 2008, which is the compiler that supports C# 3.0. Although many of the
programs in this book can be compiled by an earlier version of C#, you will need Visual C#
2008 to handle the newer features.
If you do not currently have Visual C# 2008, you will need to acquire it. Microsoft supplies
Visual C# 2008 in a variety of forms, including its commercial offerings, which can be
purchased. However, at the time of this writing, you can also obtain a copy free of charge by
downloading an Express edition. Visual C# Express edition contains a full-featured compiler
that supports all of C# 3.0 and is, therefore, able to compile all of the code in this book. It
also includes Visual Studio, which is Microsoft’s integrated development environment (IDE).
Although the Express edition does not supply all of the tools that a commercial developer will
want, it is perfect for learning C#. At the time of this writing, Visual C# 2008 Express can be
downloaded from www.microsoft.com/express. All of the code in this book has been tested
against this compiler.
Using Visual C#, there are two general approaches you can take to creating, compiling,
and running a C# program. First, you can use the Visual Studio IDE. Second, you can use the
command-line compiler, csc.exe. Both methods are described here.

---------- Post added at 08:32 PM ---------- Previous post was at 08:31 PM ----------

Using the Visual Studio IDE
As mentioned, Visual Studio is Microsoft’s integrated programming environment. It lets
you edit, compile, run, and debug a C# program, all without leaving its well thought-out
environment. Visual Studio offers convenience and helps manage your programs. It is most
effective for larger projects, but it can be used to great success with smaller programs, such as
those that constitute the examples in this book.
The steps required to edit, compile, and run a C# program using the Visual Studio 2008 IDE
are shown here. These steps assume that you are using the IDE provided by Visual C# Express
2008. Slight differences may exist with other versions of Visual Studio 2008.
1. Create a new, empty C# project by selecting File | New Project. Next, select Empty Project.

---------- Post added at 08:33 PM ---------- Previous post was at 08:32 PM ----------

Using csc.exe, the C# Command-Line Compiler
Although the Visual Studio IDE is what you will probably be using for your commercial
projects, some readers will find the C# command-line compiler more convenient, especially
for compiling and running the sample programs shown in this book. The reason is that you
don’t have to create a project for the program. You can simply create the program, then compile
it, and run it—all from the command line. Therefore, if you know how to use the Command
Prompt window and its command-line interface, using the command-line compiler will be faster
and easier than using the IDE.

---------- Post added at 08:34 PM ---------- Previous post was at 08:33 PM ----------

If you are not familiar with the Command Prompt window, it is probably better to use the
Visual Studio IDE. Although its commands are not difficult to learn, trying to learn both
the Command Prompt and C# at the same time will be a challenging experience.
To create and run programs using the C# command-line compiler, follow these three steps:
1. Enter the program using a text editor.
2. Compile the program using csc.exe.
3. Run the program.
Entering the Program
As mentioned, the programs shown in this book are available from Mcgraw-Hill’s website:
www.mhprofessional.com. However, if you want to enter the programs by hand, you are free
to do so. In this case, you must enter the program into your computer using a text editor, such
as Notepad. Remember, you must create text-only files, not formatted word-processing files,
because the format information in a word processor file will confuse the C# compiler. When
entering the program, call the file Example.cs.

---------- Post added at 08:34 PM ---------- Previous post was at 08:34 PM ----------

Compiling the Program
To compile the program, execute the C# compiler, csc.exe, specifying the name of the source
file on the command line, as shown here:
C:\>csc Example.cs
The csc compiler creates a file called Example.exe that contains the MSIL version of the
program. Although MSIL is not executable code, it is still contained in an exe file. The
Common Language Runtime automatically invokes the JIT compiler when you attempt to
execute Example.exe. Be aware, however, that if you try to execute Example.exe (or any other
exe file that contains MSIL) on a computer for which the .NET Framework is not installed, the
program will not execute, because the CLR will be missing.

zied toumi
10-19-2013, 23:40
NOTE
Prior to running csc.exe, you will need to **** a Command Prompt window that is
configured for Visual Studio. The easiest way to do this is to select “Visual Studio 2008
Command Prompt” under Visual Studio Tools in the Start menu. Alternatively, you can
start an unconfigured Command Prompt window and then run the batch file vsvars32
.bat, which is provided by Visual Studio. You may, however, encounter a problem with
the command-line approach. At the time of this writing, Visual C# 2008 Express edition
does not provide the Visual Studio Tools menu or the vsvars32.bat file. Therefore, if
you are using Visual C# Express, you may not be able to automatically configure a
Command Prompt window. In this case, use the Visual Studio IDE instead. However,
Visual C++ 2008 Express edition does supply both vsvars32.bat and the “Visual Studio
2008 Command Prompt” menu selection. Therefore, if you also install Visual C++
2008 Express Edition, you will be able to start a properly configured Command Prompt
window that will also work for C#.

---------- Post added at 08:36 PM ---------- Previous post was at 08:35 PM ----------

Running the Program
To actually run the program, just type its name on the command line, as shown here:
C:\>Example
When the program runs, the following output is displayed:
C# gives you programming power.
The First Example Program Line by Line
Although Example.cs is quite short, it includes several key features that are common to all C#
programs. Since this is your first C# program, it warrants close examination. We will begin with
its name.
The name of a C# program can be chosen arbitrarily. Unlike some computer languages
(most notably, Java) in which the name of a program file is very important, this is not the
case for C#. You were told to call the sample program Example.cs so that the instructions for
compiling and running the program would apply, but as far as C# is concerned, you could have
called the file by another name. For example, the preceding sample program could have been
called Sample.cs, Test.cs, or even MyProg.cs.
By convention, C# programs use the .cs file extension, and this is a convention that you
should follow. Also, many programmers call a file by the name of the principal class defined
within the file. This is why the filename Example.cs was chosen. Since the names of C#
programs are arbitrary, names won’t be specified for most of the sample programs in this book.
Just use names of your own choosing.
The program begins with the following lines:
/*
This is a simple C# program.
Call this program Example.cs.
*/

---------- Post added at 08:37 PM ---------- Previous post was at 08:36 PM ----------

This is a comment. Like most other programming languages, C# lets you enter remarks into
a program’s source file. The contents of a comment are ignored by the compiler. Instead, a
comment describes or explains the operation of the program to anyone who is reading its source
code. In this case, the comment describes the program and reminds you to call the source file
Example.cs. Of course, in real applications, comments generally explain how some part of the
program works or what a specific feature does.
C# supports three styles of comments. The one shown at the top of the program is called
a multiline comment. This type of comment must begin with /* and end with */. Anything
between these two comment symbols is ignored by the compiler. As the name suggests, a
multiline comment can be several lines long.
The next line in the program is
using System;

---------- Post added at 08:38 PM ---------- Previous post was at 08:37 PM ----------

This line indicates that the program is using the System namespace. In C#, a namespace defines
a declarative region. Although we will look at the namespaces in detail later, a brief description
is useful now. A namespace provides a way to keep one set of names separate from another. In
essence, names declared in one namespace will not conflict with the same names declared in
another. The namespace used by the program is System, which is the namespace reserved for
items associated with the .NET Framework class library, which is the library used by C#. The
using keyword states that the program is using the names in the given namespace.
The next line of code in the program is shown here:
class Example {
This line uses the keyword class to declare that a new class is being defined. As mentioned, the
class is C#’s basic unit of encapsulation. Example is the name of the class. The class definition
begins with the ****ing curly brace ({) and ends with the closing curly brace (}). The elements
between the two braces are members of the class. For the moment, don’t worry too much about
the details of a class, except to note that program activity occurs within one.
The next line in the program is a single-line comment, shown here:
// A C# program begins with a call to Main().
This is the second type of comment supported by C#. A single-line comment begins with
a // and ends at the end of the line. As a general rule, programmers use multiline comments for
longer remarks and single-line comments for brief, line-by-line descriptions.
The next line of code is shown here:
static void Main() {
This line begins the Main( ) method. As mentioned earlier, in C#, a subroutine is called a
method. As the comment preceding it suggests, this is the line at which the program will begin
executing. All C# applications begin execution by calling Main( ). The complete meaning of
each part of this line cannot be given now, since it involves a detailed understanding of several
other C# features. However, since many of the examples in this book will use this line of code,
a brief explanation is warranted.

---------- Post added at 08:39 PM ---------- Previous post was at 08:38 PM ----------

The line begins with the keyword static. A method that is modified by static can be called
before an object of its class has been created. This is necessary because Main( ) is called at
program startup. The keyword void indicates that Main( ) does not return a value. As you will
see, methods can also return values. The empty parentheses that follow Main indicate that no
information is passed to Main( ). As you will also see, it is possible to pass information into
Main( ) or into any other method. The last character on the line is the {. This signals the start
of Main( )’s body. All of the code that comprises a method will occur between the method’s
****ing curly brace and its closing curly brace.
The next line of code is shown here. Notice that it occurs inside Main( ).
Console.WriteLine("C# gives you programming power.");

---------- Post added at 08:39 PM ---------- Previous post was at 08:39 PM ----------

Q: You said that C# supports three types of comments, but you only mentioned two.
What is the third?
A: The third type of comment supported by C# is a documentation comment, also called
an XML comment. A documentation comment uses XML tags to help you create selfdocumenting
code.
This line outputs the string “C# gives you programming power.” followed by a new line on
the screen. Output is actually accomplished by the built-in method WriteLine( ). In this case,
WriteLine( ) displays the string that is passed to it. Information that is passed to a method
is called an argument. In addition to strings, WriteLine( ) can be used to display other types
of information, such as numeric data. The line begins with Console, which is the name of a
predefined class that supports console I/O. By connecting Console with WriteLine( ), you are
telling the compiler that WriteLine( ) is a member of the Console class. The fact that C# uses
an object to define console output is further evidence of its object-oriented nature.
Notice that the WriteLine( ) statement ends with a semicolon, as does the using System
statement earlier in the program. In general, all statements in C# end with a semicolon. The
exception to this rule is the block, which begins with { and ends with }. A block does not end
with a semicolon. This is why lines ending with } are not followed by a semicolon. Blocks
provide a mechanism for grouping statements. They are examined in detail later in this chapter.
The first } in the program ends Main( ), and the last } ends the Example class definition.

---------- Post added at 08:40 PM ---------- Previous post was at 08:39 PM ----------

One last point: C# is case-sensitive. Forgetting this can cause serious problems. For
example, if you accidentally type main instead of Main, or writeline instead of WriteLine,
the preceding program will be incorrect. Furthermore, although the C# compiler will compile
classes that do not contain a Main( ) method, there is no way to use one as an entry point for
executing your program. So, if you had mistyped Main, the compiler would still compile your
program. However, you would also see an error message that states that Example.exe does not
have an entry point defined.
Handling Syntax Errors
If you have not yet done so, enter, compile, and run the preceding program. As you may know
from your previous programming experience, it is quite easy to accidentally type something
incorrectly when entering code into your computer. If this happens, the compiler will report a
syntax error when it tries to compile it. The message displayed will contain the line number and
character position at which the error is found and a description of the error.
Although the syntax errors reported by the compiler are, obviously, helpful, they are also
sometimes misleading. The C# compiler attempts to make sense out of your source code no
matter what you have written. For this reason, the error that is reported may not always reflect

zied toumi
10-19-2013, 23:45
the actual cause of the problem. In the preceding program, for example, an accidental omission
of the ****ing curly brace after the Main( ) method generates the following sequence of
errors when compiled by the csc command-line compiler (Similar errors are generated when
compiling using the IDE.):
EX1.CS(12,21): error CS1002: ; expected
EX1.CS(13,22): error CS1519: Invalid token '(' in class, struct, or
interface member declaration
EX1.CS(15,1): error CS1022: Type or namespace definition, or
end-of-file expected
Clearly, the first error message is completely wrong, because what is missing is not a semicolon,
but a curly brace. The second two messages are equally confusing.
The point of this discussion is that when your program contains a syntax error, don’t
necessarily take the compiler’s messages at face value. They may be misleading. You may need
to “second guess” an error message in order to find the problem. Also, look at the last few lines
of code immediately preceding the one in which the error was reported. Sometimes an error will
not be reported until several lines after the point at which the error really occurred.

---------- Post added at 08:41 PM ---------- Previous post was at 08:40 PM ----------

A Small Variation
Although all of the programs in this book will use it, the statement
using System;
at the start of the first example program is not technically needed. It is, however, a valuable
convenience. The reason it’s not necessary is that in C# you can always fully qualify a name
with the namespace to which it belongs. For example, the line
Console.WriteLine("A simple C# program.");
can be rewritten as
System.Console.WriteLine("A simple C# program.");
You will occasionally see C# code that uses this approach, but it’s not common. Most C#
programmers include the using System statement at the top of their programs, as do all of the
programs in this book. Doing so avoids the tedium of always having to specify the System
namespace whenever a member of that namespace is used. It is important to understand,
however, that you can explicitly qualify a name with its namespace if needed.
Using a Variable
Perhaps no other construct is as important to a programming language as the variable. A variable
is a named memory location that can be assigned a value. It is called a variable because its value
can be changed during the execution of a program. In other words, the contents of a variable are
changeable, not fixed.

---------- Post added at 08:42 PM ---------- Previous post was at 08:41 PM ----------

Let’s begin with an example. The following program creates three variables called length,
width, and area. It uses these variables to compute and display the area of a rectangle that has
the dimensions 9 by 7.
// This program introduces variables.
using System;
class UseVars {
static void Main() {
int length; // this declares a variable
int width; // this declares another variable
int area; // this is a third variable
// Assign length the value 9.
length = 9;
// This displays the current value of length.
Console.WriteLine("length contains " + length);
// Assign width the value 7.
width = 7;
// This displays the current value of width.
Console.WriteLine("width contains " + width);
// Assign to area the product of length and width.
area = length * width;
// Display the result.
Console.Write("area contains length * width: ");
Console.WriteLine(area);
}
}

---------- Post added at 08:42 PM ---------- Previous post was at 08:42 PM ----------

When you run this program, you will see the following output:
length contains 9
width contains 7
area contains length * width: 63
This program introduces several new concepts. First, the statement
int length; // this declares a variable
declares a variable called length of type integer. In C#, all variables must be declared before
they are used. Further, the kind of values that the variable can hold must also be specified.
This is called the type of the variable. In this case, length can hold integer values. These are
whole numbers. In C#, to declare a variable to be of type integer, precede its name with the
keyword int. Thus, the preceding statement declares a variable called length of type int.
The next two lines declare two more int variables, called width and area:
int width; // this declares another variable
int area; // this is a third variable
Notice that each uses the same format as the first, except that the name of the variable is
different.
In general, to declare a variable, you will use a statement like this:
type var-name;
Here, type specifies the type of variable being declared, and var-name is the name of the
variable. In addition to int, C# supports several other data types, such as double, char, and
string.
The following line of code assigns length the value 9:
length = 9;

---------- Post added at 08:43 PM ---------- Previous post was at 08:42 PM ----------

In C#, the assignment operator is the single equal sign. It copies the value on its right side into
the variable on its left.
The next line of code outputs the value of length preceded by the string “length contains.”
Console.WriteLine("length contains " + length);
In this statement, the plus sign causes the value of length to be displayed after the string that
precedes it. This approach can be generalized. Using the + operator, you can chain together as
many items as you want within a single WriteLine( ) statement.
Next, the value of width is set to 7 and displayed, using the same type of statements just
described. Then, the following line of code assigns area the value of length times width.
area = length * width;
This line multiplies the value in length (which is 9) by the value in width (which is 7), and
then stores that result in area. Thus, after the line executes, area will contain the value 63. The
values of length and width will be unchanged.
Like most other computer languages, C# supports a complete set of arithmetic operators,
including those shown here:
+ Addition
− Subtraction
* Multiplication
/ Division
These can be used with any type of numeric data

---------- Post added at 08:44 PM ---------- Previous post was at 08:43 PM ----------

Q: You say that all variables must be declared before they are used, and that all variables
have a type. However, you mention that C# 3.0 includes a new feature called an implicitly
typed variable. What is this, and does it circumvent the need to declare variables?
A: As you will learn in Chapter 2, implicitly typed variables are variables whose type is
automatically determined by the compiler. Understand, however, that an implicitly typed
variable still needs to be declared. Instead of using a type name, such as int, an implicitly
typed variable is declared using the var keyword. Implicitly typed variables are very
useful in several specialized situations (especially those involving LINQ), but they are not
intended to replace explicitly typed variables in general. Normally, when you declare a
variable, you give it an explicit type.

---------- Post added at 08:44 PM ---------- Previous post was at 08:44 PM ----------

Here are the next two lines in the program:
Console.Write("area contains length * width: ");
Console.WriteLine(area);
Two new things are occurring here. First, the built-in method Write( ) is used to display the
string “area contains length * width:”. This string is not followed by a new line. This means
that when the next output is generated, it will start on the same line. The Write( ) method is just
like WriteLine( ), except that it does not output a new line after each call. Second, in the call to
WriteLine( ), notice that area is used by itself. Both Write( ) and WriteLine( ) can be used to
output values of any of C#’s built-in types.
One more point about declaring variables before we move on: It is possible to declare two
or more variables using the same declaration statement. Just separate their names by commas.
For example, length and width could have been declared like this:
int length, width; // both length and width declared using one statement
The double Data Type
In the preceding program, a variable of type int was used. However, a variable of type int can
hold only whole numbers. Thus, it cannot be used when a fractional component is required. For
example, an int variable can hold the value 18, but not the value 18.3. Fortunately, int is only
one of several data types defined by C#. To allow numbers with fractional components, C#
defines two floating-point types: float and double, which represent single- and double-precision
values, respectively. Of the two, double is the most commonly used.
To declare a variable of type double, use a statement similar to that shown here:
double result;

---------- Post added at 08:45 PM ---------- Previous post was at 08:44 PM ----------

Here, result is the name of the variable, which is of type double. Because result has a floatingpoint
type, it can hold values such as 122.23, 0.034, and –19.0.
To better understand the difference between int and double, try the following program:
/*
This program illustrates the differences
between int and double.
*/
using System;
class IntVsDouble {
static void Main() {
int ivar; // this declares an int variable
double dvar; // this declares a floating-point variable
// Assign ivar the value 10.
ivar = 10;
// Assign dvar the value 10.0.
dvar = 10.0;
Console.WriteLine("Original value of ivar: " + ivar);
Console.WriteLine("Original value of dvar: " + dvar);

zied toumi
10-19-2013, 23:48
Console.WriteLine(); // print a blank line
// Now, divide both by 4.
ivar = ivar / 4;
dvar = dvar / 4.0;
Console.WriteLine("ivar after division: " + ivar);
Console.WriteLine("dvar after division: " + dvar);
}
}
The output from this program is shown here:
Original value of ivar: 10
Original value of dvar: 10
ivar after division: 2
dvar after division: 2.5
As you can see, when ivar is divided by 4, a whole-number division is performed and the
outcome is 2—the fractional component is lost. However, when dvar is divided by 4.0, the
fractional component is preserved and the outcome is 2.5.

---------- Post added at 08:47 PM ---------- Previous post was at 08:46 PM ----------

Q: Why does C# have different data types for integers and floating-point values? That is,
why aren’t all numeric values just the same type?
A: C# supplies different data types so that you can write efficient programs. For example,
integer arithmetic is faster than floating-point calculations. Thus, if you don’t need
fractional values, you don’t need to incur the overhead associated with types float or
double. Secondly, the amount of memory required for one type of data might be less than
that required for another. By supplying different types, C# enables you to make the best
use of system resources. Finally, some algorithms require (or at least benefit from) the use
of a specific type of data. C# supplies a number of built-in types to give you the greatest
flexibility.

---------- Post added at 08:48 PM ---------- Previous post was at 08:47 PM ----------

As the program shows, when you want to specify a floating-point value in a program, it
must include a decimal point. If you don’t, it will be interpreted as an integer. For example, in
C#, the value 100 is an integer, but the value 100.0 is a floating-point value.
There is one other new thing to notice in the program. To print a blank line, simply call
WriteLine( ) without any arguments.
Convert Fahrenheit to Celsius
Although the preceding sample programs illustrate several important features of the C#
language, they are not very useful. Even though you do not know much about C# at this point,
you can still put what you have learned to work to create a practical program. Here you will
create a program that converts Fahrenheit to Celsius.
The program declares two double variables. One will hold the number of the degrees in
Fahrenheit, and the second will hold the number of degrees in Celsius after the conversion. The
formula for converting Fahrenheit to Celsius is
C = 5/9 * (F – 32)
where C is the temperature in degrees Celsius and F is the temperature in degrees Fahrenheit.
Step by Step
1. Create a new C# file called FtoC.cs. (If you are using the Visual Studio IDE rather than the
command-line compiler, you will need to add this file to a C# project, as described earlier.)
2. Enter the following program into the file:
/*
This program converts Fahrenheit to Celsius.

maha elsayed
10-19-2013, 23:49
السلام عليكم ورحمه الله وبركاته اشكرك يا اخى ع هذا الكتاب الرائع
تقبل مرورى ارجوك واكيد هقراه

zied toumi
10-19-2013, 23:59
Call this program FtoC.cs.
*/
using System;
class FtoC {
static void Main() {
double f; // holds the temperature in Fahrenheit
double c; // holds the temperature in Celsius
// Begin with 59 degrees Fahrenheit
f = 59.0;
// convert to Celsius
c = 5.0 / 9.0 * (f - 32.0);
Console.Write(f + " degrees Fahrenheit is ");
Console.WriteLine(c + " degrees Celsius.");
}
}
3. Compile the program using the Visual Studio IDE (using the instructions shown earlier in
this chapter) or by using the following command line:
C>csc FtoC.cs
4. Run the program. You will see this output:
59 degrees Fahrenheit is 15 degrees Celsius.
5. As it stands, this program converts 59 degrees Fahrenheit to Celsius. However, by changing
the value assigned to f, you can have the program convert a different temperature. Notice that
the variables f and c are of type double. This is necessary because the fractional component
of the operations is needed to ensure an accurate conversion.

---------- Post added at 08:50 PM ---------- Previous post was at 08:49 PM ----------

Two Control Statements
Inside a method, execution proceeds from one statement to the next, top to bottom. However, it
is possible to alter this flow through the use of the various program control statements supported
by C#. Although we will look closely at control statements in Chapter 3, two are briefly
introduced here because we will be using them to write sample programs.
The if Statement
You can selectively execute part of a program through the use of C#’s conditional statement: the
if. The if statement works in C# much like the IF statement in any other language. For example,

---------- Post added at 08:51 PM ---------- Previous post was at 08:50 PM ----------

it is syntactically similar to the if statements in C, C++, and Java. Its simplest form is shown
here:
if(condition) statement;
Here, condition is a Boolean (that is, true or false) expression. If condition is true, then the
statement is executed. If condition is false, then the statement is bypassed. Here is an example:
if(10 < 11) Console.WriteLine("10 is less than 11");
In this case, since 10 is less than 11, the conditional expression is true, and WriteLine( ) will
execute. However, consider the following:
if(10 < 9) Console.WriteLine("this won't be displayed");
In this case, 10 is not less than 9. Thus, the call to WriteLine( ) will not take place.
C# defines a full complement of relational operators that can be used in a conditional
expression. They are shown here:

---------- Post added at 08:52 PM ---------- Previous post was at 08:51 PM ----------

Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
= = Equal to
!= Not equal to
Notice that the test for equality is the double equal sign.
Here is a program that illustrates the if statement and the relational operators.
// Demonstrate the if.
using System;
class IfDemo {
static void Main() {
int a, b, c;
a = 2;
b = 3;
// This if statement succeeds because a is less than b.
if(a < b) Console.WriteLine("a is less than b");
The WriteLine( ) statement is executed
only when the condition is true.
This is the condition tested by the if.

---------- Post added at 08:52 PM ---------- Previous post was at 08:52 PM ----------

The output generated by this program is shown here:
a is less than b
a contains the value 2
a equals b - 1
c contains -1
c is negative
c now contains 1
c is non-negative
Notice one other thing in this program. The line
int a, b, c;

---------- Post added at 08:53 PM ---------- Previous post was at 08:52 PM ----------

declares three variables, a, b, and c, by use of a comma-separated list. As mentioned earlier,
when you need two or more variables of the same type, they can be declared in one statement.
Just separate the variable names by commas.
The for Loop
You can repeatedly execute a sequence of code by creating a loop. C# supplies a powerful
assortment of loop constructs. The one we will look at here is the for loop. If you are familiar
with C, C++, or Java, you will be pleased to know that the for loop in C# works the same way it
does in those languages. The simplest form of the for loop is shown here:
for(initialization; condition; iteration) statement;
In its most common form, the initialization portion of the loop sets a loop control variable
to an initial value. The condition is a Boolean expression that tests the loop control variable.
If the outcome of that test is true, the for loop continues to iterate. If it is false, the loop
terminates. The iteration expression determines how the loop control variable is changed each
time the loop iterates. Here is a short program that illustrates the for loop:
// Demonstrate the for loop.

---------- Post added at 08:54 PM ---------- Previous post was at 08:53 PM ----------

using System;
class ForDemo {
static void Main() {
int count;
Console.WriteLine("Counting from 0 to 4:");
for(count = 0; count < 5; count = count+1)
Console.WriteLine(" count is " + count);
Console.WriteLine("Done!");
}
}
The output generated by the program is shown here:
Counting from 0 to 4:
count is 0
count is 1
count is 2
count is 3
count is 4
Done!
In this example, count is the loop control variable. It is set to zero in the initialization portion
of the for. At the start of each iteration (including the first one), the conditional test count < 5 is
performed. If the outcome of this test is true, the WriteLine( ) statement is executed, and then

---------- Post added at 08:57 PM ---------- Previous post was at 08:54 PM ----------

the iteration portion of the loop is executed. This process continues until the conditional test is
false, at which point execution picks up at the bottom of the loop.
As a point of interest, in professionally written C# programs, you will almost never see the
iteration portion of the loop written as shown in the preceding program. That is, you will seldom
see statements like this:
count = count + 1;
The reason is that C# includes a special increment operator that performs this operation more
compactly. The increment operator is ++ (two consecutive plus signs). The increment operator
increases its operand by one. By use of the increment operator, the preceding statement can be
written like this:
count++;
Thus, the for in the preceding program will usually be written like this:
for(count = 0; count < 5; count++)
You might want to try this. As you will see, the loop still runs exactly the same as it did before.
C# also provides a decrement operator, which is specified as – – (two consecutive minus
signs). This operator decreases its operand by one.

---------- Post added at 08:57 PM ---------- Previous post was at 08:57 PM ----------

Using Blocks of Code
Another key element of C# is the code block. A code block is a grouping of statements. A code
block is created by enclosing the statements between ****ing and closing curly braces. Once a
block of code has been created, it becomes a logical unit that can be used any place that a single
statement can. For example, a block can be a target for if and for statements. Consider this if
statement:
if(counter < max) {
usercount = counter;
delaytime = 0;
}
Here, if counter is less than max, both statements inside the block will be executed. Thus, the
two statements inside the block form a logical unit, and one statement cannot execute without
the other also executing. The key point here is that whenever you need to logically link two
or more statements, you do so by creating a block. Code blocks allow many algorithms to be
implemented with greater clarity and efficiency.
Here is a program that uses a block of code to prevent a division by zero:
// Demonstrate a block of code.

---------- Post added at 08:58 PM ---------- Previous post was at 08:57 PM ----------

class BlockDemo {
static void Main() {
double i, j, d;
i = 5.0;
j = 10.0;
// The target of this if is a block.
if(i != 0) {
Console.WriteLine("i does not equal zero");
d = j / i;
Console.WriteLine("j / i is " + d);
}
}
}
The output generated by this program is shown here:
i does not equal zero
j / i is 2
In this case, the target of the if statement is a block of code and not just a single statement. If the
condition controlling the if is true (as it is in this case), the three statements inside the block will
be executed. Try setting i to zero and observe the result.
As you will see later in this book, blocks of code have additional properties and uses.
However, the main reason for their existence is to create logically inseparable units of code.

---------- Post added at 08:59 PM ---------- Previous post was at 08:58 PM ----------

Semicolons and Positioning
In C#, the semicolon signals the end of a statement. That is, each individual statement must end
with a semicolon.
As you know, a block is a set of logically connected statements that are surrounded by
****ing and closing braces. A block is not terminated with a semicolon. Since a block is a group
of statements, it makes sense that a block is not terminated by a semicolon; instead, the end of
the block is indicated by the closing brace.
Q: Does the use of a code block introduce any runtime inefficiencies? In other words, do
the { and } consume any extra time during the execution of my program?
A: No. Code blocks do not add any overhead whatsoever. In fact, because of their ability to
simplify the coding of certain algorithms, their use generally increases speed and efficiency.

zied toumi
10-20-2013, 00:05
C# does not recognize the end of the line as the end of a statement—only a semicolon
terminates a statement. For this reason, it does not matter where on a line you put a statement.
For example, to C#,
x = y;
y = y + 1;
Console.WriteLine(x + " " + y);
is the same as
x = y; y = y + 1; Console.WriteLine(x + " " + y);
Furthermore, the individual elements of a statement can also be put on separate lines. For
example, the following is perfectly acceptable:
Console.WriteLine("This is a long line of output" +
x + y + z +
"more output");
Breaking long lines in this fashion is often used to make programs more readable. It can also
help prevent excessively long lines from wrapping.

---------- Post added at 09:00 PM ---------- Previous post was at 08:59 PM ----------

Indentation Practices
You may have noticed in the previous examples that certain statements were indented. C#
is a free-form language, meaning that it does not matter where you place statements relative
to each other on a line. However, over the years, a common and accepted indentation style
has developed that allows for very readable programs. This book follows that style, and it is
recommended that you do so as well. Using this style, you indent one level after each ****ing
brace, and move back out one level after each closing brace. There are certain statements that
encourage some additional indenting; these will be covered later.
Improve the Temperature Conversion Program
You can use the for loop, the if statement, and code blocks to create an improved version of the
Fahrenheit-to-Celsius converter that you developed in the first Try This example. This new version
will print a table of conversions, beginning with 0 degrees Fahrenheit and ending with 99. After
every 10 degrees, a blank line will be output. This is accomplished through the use of a variable
called counter that counts the number of lines that have been output. *** special attention to its use.

---------- Post added at 09:01 PM ---------- Previous post was at 09:00 PM ----------

Step by Step
1. Create a new file called FtoCTable.cs.
2. Enter the following program into the file:
/*
This program displays a conversion
table of Fahrenheit to Celsius.
Call this program FtoCTable.cs.
*/
using System;
class FtoCTable {
static void Main() {
double f, c;
int counter;
counter = 0;
for(f = 0.0; f < 100.0; f++) {
// Convert to Celsius
c = 5.0 / 9.0 * (f - 32.0);
Console.WriteLine(f + " degrees Fahrenheit is " +
c + " degrees Celsius.");
counter++;
// Every 10th line, print a blank line.
if(counter == 10) {
Console.WriteLine();
counter = 0; // reset the line counter
}
}
}
}
3. Compile the program as described earlier.
4. Run the program. Here is a portion of the output that you will see. Notice how a blank line
is output every tenth line. As mentioned, this is controlled by the counter variable, which is
initially set to zero. Each time through the for loop, counter is incremented. When counter
equals 10, a blank line is output and then counter is reset to zero. This process causes the
output to be grouped into lines of ten units.
0 degrees Fahrenheit is -17.7777777777778 degrees Celsius.
1 degrees Fahrenheit is -17.2222222222222 degrees Celsius.

---------- Post added at 09:02 PM ---------- Previous post was at 09:01 PM ----------

2 degrees Fahrenheit is -16.6666666666667 degrees Celsius.
3 degrees Fahrenheit is -16.1111111111111 degrees Celsius.
4 degrees Fahrenheit is -15.5555555555556 degrees Celsius.
5 degrees Fahrenheit is -15 degrees Celsius.
6 degrees Fahrenheit is -14.4444444444444 degrees Celsius.
7 degrees Fahrenheit is -13.8888888888889 degrees Celsius.
8 degrees Fahrenheit is -13.3333333333333 degrees Celsius.
9 degrees Fahrenheit is -12.7777777777778 degrees Celsius.
10 degrees Fahrenheit is -12.2222222222222 degrees Celsius.
11 degrees Fahrenheit is -11.6666666666667 degrees Celsius.
12 degrees Fahrenheit is -11.1111111111111 degrees Celsius.
13 degrees Fahrenheit is -10.5555555555556 degrees Celsius.
14 degrees Fahrenheit is -10 degrees Celsius.
15 degrees Fahrenheit is -9.44444444444444 degrees Celsius.
16 degrees Fahrenheit is -8.88888888888889 degrees Celsius.
17 degrees Fahrenheit is -8.33333333333333 degrees Celsius.
18 degrees Fahrenheit is -7.77777777777778 degrees Celsius.
19 degrees Fahrenheit is -7.22222222222222 degrees Celsius.

---------- Post added at 09:02 PM ---------- Previous post was at 09:02 PM ----------

The C# Keywords
At its foundation, a computer language is defined by its keywords, and C# provides a rich and
diverse set. Furthermore, C# defines two general types of keywords: reserved and contextual.
The reserved keywords cannot be used as names for variables, classes, or methods. They can
be used only as keywords. This is why they are called reserved. The terms reserved words or
reserved identifiers are also sometimes used. There are currently 77 reserved keywords defined
by version 3.0 of the C# language. They are shown in Table 1-1.
C# 3.0 defines 13 contextual keywords that have a special meaning in certain contexts. In
those contexts, they act as keywords. Outside their context, they can be used as names for other
program elements, such as variable names. Thus, they are not technically reserved. As a general
rule, however, you should consider the contextual keywords reserved and avoid using them for
any other purpose. Using a contextual keyword as a name for some other program element can
be confusing and is considered bad practice by many programmers. The contextual keywords
are shown in Table 1-2.
Identifiers
In C#, an identifier is a name assigned to a method, a variable, or any other user-defined item.
Identifiers can be from one to several characters long. Variable names may start with any
letter of the alphabet or with an underscore. Next may be a letter, a digit, or an underscore.
The underscore can be used to enhance the readability of a variable name, as in line_count.

---------- Post added at 09:03 PM ---------- Previous post was at 09:02 PM ----------

Uppercase and lowercase are different; that is, to C#, myvar and MyVar are separate names.
Here are some examples of acceptable identifiers:
Test x y2 MaxLoad
up _top my_var sample23
Remember that you can’t start an identifier with a digit. Thus, 12x is invalid, for example.
Good programming practice dictates that you use identifier names that reflect the meaning or
usage of the items being named.
Although you cannot use any of the C# keywords as identifiers, C# does allow you to precede
a keyword with an @, allowing it to be a legal identifier. For example, @for is a valid identifier. In
this case, the identifier is actually for and the @ is ignored. Frankly, using @-qualified keywords
for identifiers is not recommended, except for special purposes. Also, the @ can precede any
identifier, but this is considered bad practice.

---------- Post added at 09:05 PM ---------- Previous post was at 09:03 PM ----------

The C# Class Library
The sample programs shown in this chapter make use of two of C#’s built-in methods:
WriteLine( ) and Write( ). As mentioned, these methods are members of the Console class,
which is part of the System namespace, which is defined by the .NET Framework’s class
library. As explained earlier in this chapter, the C# environment relies on the .NET Framework
class library to provide support for such things as I/O, string handling, networking, and GUIs.
Thus, C# as a totality is a combination of the C# language itself, plus the .NET standard
classes. As you will see, the class library provides much of the functionality that is part of any
C# program. Indeed, part of becoming a C# programmer is learning to use the standard library.
Throughout this book, various elements of the .NET library classes and methods are described.
However, the .NET library is quite large, and it is something you will also want to explore
more on your own.
Chapter 1 Self Test
1. What is the MSIL and why is it important to C#?
2. What is the Common Language Runtime?
3. What are the three main principles of object-oriented programming?
4. Where do C# programs begin execution?
5. What is a variable? What is a namespace?
6. Which of the following variable names is invalid?
A. count
B. $count
C. count27
D. 67count
E. @if
7. How do you create a single-line comment? How do you create a multiline comment?
8. Show the general form of the if statement. Show the general form of the for loop.
9. How do you create a block of code?
10. Is it necessary to start each C# program with the following statement?
using System;
11. The moon’s gravity is about 17 percent that of Earth’s. Write a program that computes your
effective weight on the moon.
12. Adapt the FtoCTable program in “Try This: Improve the Temperature Conversion Program”
so that it prints a conversion table of inches to meters. Display 12 feet of conversions, inch by
inch. Output a blank line every 12 inches. (One meter equals approximately 39.37 inches.)

zied toumi
10-20-2013, 00:14
Why Data Types Are Important
Data types are especially important in C# because it is a strongly typed language. This means
that all operations are type-checked by the compiler for type compatibility. Illegal operations
will not be compiled. Thus, strong type-checking helps prevent errors and enhances reliability.
To enable strong type-checking, all variables, expressions, and values have a type. There is no
concept of a “typeless” variable, for example. Furthermore, the type of a value determines what
operations are allowed on it. An operation allowed on one type might not be allowed on another.
C#’s Value Types
C# contains two general categories of built-in data types: value types and reference types. The
difference between the two types is what a variable contains. For a value type, a variable holds
an actual value, such 101 or 98.6. For a reference type, a variable holds a reference to the value.

---------- Post added at 09:06 PM ---------- Previous post was at 09:05 PM ----------

The most commonly used reference type is the class, and a discussion of classes and reference
types is deferred until later. The value types are described here.
At the core of C# are the 13 value types shown in Table 2-1. Collectively, these are
referred to as the simple types. They are called simple types because they consist of a single
value. (In other words, they are not a composite of two or more values.) They form the
foundation of C#’s type system, providing the basic, low-level data elements upon which
a program operates. The simple types are also sometimes referred to as primitive types.
C# strictly specifies a range and behavior for each simple type. Because of portability
requirements and to support mixed-language programming, C# is uncompromising on this
account. For example, an int is the same in all execution environments. There is no need to
rewrite code to fit a specific platform. While strictly specifying the size of the simple types
may cause a small loss of performance in some environments, it is necessary in order to
achieve portability.
NOTE
In addition to the simple types, C# defines three other categories of value types. These
are enumerations, structures, and nullable types, all of which are described later in
this book.
Integers
C# defines nine integer types: char, byte, sbyte, short, ushort, int, uint, long, and ulong.
However, the char type is primarily used for representing characters, and it is discussed later

---------- Post added at 09:11 PM ---------- Previous post was at 09:06 PM ----------

Type Meaning
bool Represents true/false values
byte 8-bit unsigned integer
char Character
decimal Numeric type for financial calculations
double Double-precision floating point
float Single-precision floating point
int Integer
long Long integer
sbyte 8-bit signed integer
short Short integer
uint Unsigned integer
ulong Unsigned long integer
ushort Unsigned short integer
Table 2-1 The C# Simple Types

---------- Post added at 09:12 PM ---------- Previous post was at 09:11 PM ----------

in this chapter. The remaining eight integer types are used for numeric calculations. Their bitwidth
and ranges are shown here:
Type Width in Bits Range
byte 8 0 to 255
sbyte 8 –128 to 127
short 16 –32,768 to 32,767
ushort 16 0 to 65,535
int 32 –2,147,483,648 to 2,147,483,647
uint 32 0 to 4,294,967,295
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong 64 0 to 18,446,744,073,709,551,615
As the table shows, C# defines both signed and unsigned versions of the various integer
types. The difference between signed and unsigned integers is in the way the high-order bit of
the integer is interpreted. If a signed integer is specified, the C# compiler will generate code
that assumes the high-order bit of an integer is to be used as a sign flag. If the sign flag is 0,
the number is positive; if it is 1, the number is negative. Negative numbers are almost always
represented using the two’s complement approach. In this method, all bits in the number are
reversed, and then 1 is added to this number.
Signed integers are important for a great many algorithms, but they have only half the
absolute magnitude of their unsigned relatives. For example, as a short, here is 32,767:
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

---------- Post added at 09:12 PM ---------- Previous post was at 09:12 PM ----------

For a signed value, if the high-order bit were set to 1, the number would then be interpreted
as –1 (assuming the two’s complement format). However, if you declared this to be a ushort,
then when the high-order bit was set to 1, the number would become 65,535.
Probably the most commonly used integer type is int. Variables of type int are often
employed to control loops, to index arrays, and for general-purpose integer math. When you
need an integer that has a range greater than int, you have many options. If the value you want
to store is unsigned, you can use uint. For large signed values, use long. For large unsigned
values, use ulong.
Here is a program that computes the number of cubic inches contained in a cube that is
1 mile long on each side. Because this value is so large, the program uses a long variable to
hold it.
// Compute the number of cubic inches in 1 cubic mile.
using System;
class Inches {
static void Main() {
long ci;
long im;
im = 5280 * 12;
ci = im * im * im;
Console.WriteLine("There are " + ci +
" cubic inches in cubic mile.");
}
}
Here is the output from the program:
There are 254358061056000 cubic inches in cubic mile.
Clearly, the result could not have been held in an int or uint variable.
The smallest integer types are byte and sbyte. The byte type is an unsigned value between
0 and 255. Variables of type byte are especially useful when working with raw binary data,
such as a byte stream produced by some device. For small signed integers, use sbyte. Here is
an example that uses a variable of type byte to control a for loop that produces the summation
of the number 100:

---------- Post added at 09:13 PM ---------- Previous post was at 09:12 PM ----------

// Use byte.
using System;
class Use_byte {
static void Main() {
byte x;
int sum;
sum = 0;
for(x = 1; x <= 100; x++)
sum = sum + x;
Console.WriteLine("Summation of 100 is " + sum);
}
}
The output from the program is shown here:
Summation of 100 is 5050
Since the for loop runs only from 0 to 100, which is well within the range of a byte, there is no
need to use a larger type variable to control it. Of course, byte could not have been used to hold
the result of the summation because 5050 is far outside its range. This is why sum is an int.
When you need an integer that is larger than a byte or sbyte but smaller than an int or
uint, use short or ushort.

---------- Post added at 09:13 PM ---------- Previous post was at 09:13 PM ----------

Floating-Point Types
As explained in Chapter 1, the floating-point types can represent numbers that have fractional
components. There are two kinds of floating-point types, float and double, which represent
single- and double-precision numbers, respectively. The type float is 32 bits wide and has a
range of 1.5E–45 to 3.4E+38. The double type is 64 bits wide and has a range of 5E–324 to
1.7E+308.
Of the two, double is the most commonly used. One reason for this is that many of the
math functions in C#’s class library (which is the .NET Framework library) use double values.
For example, the Sqrt( ) method (which is defined by the System.Math class) returns a double
value that is the square root of its double argument. Here, Sqrt( ) is used to compute the length
of the hypotenuse given the lengths of the two opposing sides:
/*
Use the Pythagorean theorem to find the length of the hypotenuse
given the lengths of the two opposing sides.
*/
using System;
class Hypot {
static void Main() {
double x, y, z;
x = 3;
y = 4;
z = Math.Sqrt(x*x + y*y);
Console.WriteLine("Hypotenuse is " + z);
}
}
The output from the program is shown here:
Hypotenuse is 5
Here’s another point about the preceding example. As mentioned, Sqrt( ) is a member of
the Math class. Notice how Sqrt( ) is called; it is preceded by the name Math. This is similar
to the way Console precedes WriteLine( ). Although not all standard methods are called by
specifying their class name first, several are.
The decimal Type
Perhaps the most interesting C# numeric type is decimal, which is intended for use in
monetary calculations. The decimal type utilizes 128 bits to represent values within the range
1E–28 to 7.9E+28. As you may know, normal floating-point arithmetic is subject to a variety

---------- Post added at 09:14 PM ---------- Previous post was at 09:13 PM ----------

of rounding errors when it is applied to decimal values. The decimal type eliminates these
errors and can accurately represent up to 28 decimal places (or 29 places, in some cases).
This ability to represent decimal values without rounding errors makes it especially useful for
computations that involve money.
Here is a program that uses the decimal type in a financial calculation. The program
computes a balance after interest has been applied.
// Use the decimal type in a financial calculation.
using System;
class UseDecimal {
static void Main() {
decimal balance;
decimal rate;
// Compute new balance.
balance = 1000.10m;
rate = 0.1m;
balance = balance * rate + balance;
Console.WriteLine("New balance: $" + balance);
}
}
The output from this program is shown here:
New balance: $1100.110

zied toumi
10-20-2013, 00:18
In the program, notice that the decimal constants are followed by the m or M suffix. This
is necessary because without the suffix, these values would be interpreted as standard floatingpoint
constants, which are not compatible with the decimal data type. (We will look more
closely at how to specify numeric constants later in this chapter.)
Q: The other computer languages that I have worked with do not have a decimal data
type. Is it unique to C#?
A: The decimal type is not supported by C, C++, or Java as a built-in type. Thus, within its
direct line of descent, it is unique.
Characters
In C#, characters are not 8-bit quantities like they are in many other computer languages, such
as C++. Instead, C# uses Unicode. Unicode defines a character set that can represent all of the
characters found in all human languages. Thus, in C#, char is an unsigned 16-bit type having a
range of 0 to 65,535. The standard 8-bit ASCII character set is a subset of Unicode and ranges
from 0 to 127. Thus, the ASCII characters are still valid C# characters.
A character variable can be assigned a value by enclosing the character inside single
quotes. For example, this assigns X to the variable ch:
char ch;
ch = 'X';
You can output a char value using a WriteLine( ) statement. For example, this line
outputs the value in ch:
Console.WriteLine("This is ch: " + ch);
Although char is defined by C# as an integer type, it cannot be freely mixed with integers
in all cases. This is because there is no automatic type conversion from integer to char. For
example, the following fragment is invalid:
char ch;
ch = 10; // error, won't work
The reason the preceding code will not work is that 10 is an integer value and it won’t
automatically convert to a char. Thus, the assignment involves incompatible types. If you
attempt to compile this code, you will see an error message. Later in this chapter you will see
a way around this restriction.

---------- Post added at 09:16 PM ---------- Previous post was at 09:15 PM ----------

Q: Why does C# use Unicode?
A: C# was designed to allow programs to be written for worldwide use. Thus, it needs to use
a character set that can represent all of the world’s languages. Unicode is the standard
character set designed expressly for this purpose. Of course, the use of Unicode is
inefficient for languages such as English, German, Spanish, or French, whose characters
can be contained within 8 bits. But such is the price of global portability.
The bool Type
The bool type represents true/false values. C# defines the values true and false using the
reserved words true and false. Thus, a variable or expression of type bool will be one of these
two values. Furthermore, there is no conversion defined between bool and integer values. For
example, 1 does not convert to true, and 0 does not convert to false.
Here is a program that demonstrates the bool type:
// Demonstrate bool values.
using System;
class BoolDemo {
static void Main() {
bool b;
b = false;
Console.WriteLine("b is " + b);
b = true;
Console.WriteLine("b is now " + b);
// A bool value can control the if statement.
if(b) Console.WriteLine("This is executed.");
b = false;
if(b) Console.WriteLine("This is not executed.");
// The outcome of a relational operator is a bool value.
Console.WriteLine("88 > 17 is " + (88 > 17));
}
}

---------- Post added at 09:16 PM ---------- Previous post was at 09:16 PM ----------

The output generated by this program is shown here:
b is False
b is now True
This is executed.
88 > 17 is True
There are three interesting things to notice about this program. First, as you can see, when
a bool value is output by WriteLine( ), “True” or “False” is displayed. Second, the value of a
bool variable is sufficient, by itself, to control the if statement. There is no need to write an if
statement like this:
if(b == true) ...
Third, the outcome of a relational operator, such as <, is a bool value. This is why the expression
88 > 17 displays the value “True.” Further, the extra set of parentheses around 88 > 17 is
necessary because the + operator has a higher precedence than the > operator.

---------- Post added at 09:17 PM ---------- Previous post was at 09:16 PM ----------

Some Output Options
Before continuing our examination of data types and operators, a small digression will be
useful. Up to this point, when outputting lists of data, you have been separating each part of
the list with a plus sign, as shown here:
Console.WriteLine("You ordered " + 2 + " items at $" + 3 + " each.");
Although very convenient, outputting numeric information in this way does not give you
any control over how that information appears. For example, for a floating-point value, you
can’t control the number of decimal places displayed. Consider the following statement:
Console.WriteLine("Here is 10/3: " + 10.0/3.0);
It generates this output:
Here is 10/3: 3.33333333333333
While this might be fine for some purposes, displaying so many decimal places could be
inappropriate for others. For example, in financial calculations, you will usually want to display
two decimal places.
To control how numeric data is formatted, you will need to use a second form of WriteLine( ),
shown here, which allows you to embed formatting information:
WriteLine(“format string”, arg0, arg1, ... , argN)
In this version, the arguments to WriteLine( ) are separated by commas and not plus signs.
The format string contains two items: regular, printing characters that are displayed as-is and
format specifiers. Format specifiers take this general form:
{argnum, width: fmt}

---------- Post added at 09:17 PM ---------- Previous post was at 09:17 PM ----------

Here, argnum specifies the number of the argument (starting from zero) to display. The minimum
width of the field is specified by width, and the format is specified by fmt.
During execution, when a format specifier is encountered in the format string, the
corresponding argument, as specified by argnum, is substituted and displayed. Thus, it is the
position of a format specification within the format string that determines where its matching
data will be displayed. Both width and fmt are optional. Thus, in its simplest form, a format
specifier simply indicates which argument to display. For example, {0} indicates arg0, {1}
specifies arg1, and so on.
Let’s begin with a simple example. The statement
Console.WriteLine("February has {0} or {1} days.", 28, 29);
produces the following output:
February has 28 or 29 days.
As you can see, the value 28 is substituted for {0}, and 29 is substituted for {1}. Thus, the
format specifiers identify the location at which the subsequent arguments—in this case,

---------- Post added at 09:18 PM ---------- Previous post was at 09:17 PM ----------

28 and 29—are displayed within the string. Furthermore, notice that the additional values are
separated by commas, not plus signs.
Here is a variation of the preceding statement that specifies minimum field widths:
Console.WriteLine("February has {0,10} or {1,5} days.", 28, 29);
It produces the following output:
February has 28 or 29 days.
As you can see, spaces have been added to fill out the unused portions of the fields. Remember,
a minimum field width is just that: the minimum width. Output can exceed that width if needed.
In the preceding examples, no formatting was applied to the values themselves. Of course,
the value of using format specifiers is to control the way the data looks. The types of data most
commonly formatted are floating-point and decimal values. One of the easiest ways to specify
a format is to describe a template that WriteLine( ) will use. To do this, show an example of
the format that you want, using #s to mark the digit positions. For instance, here is a better way
to display 10 divided by 3:
Console.WriteLine("Here is 10/3: {0:#.##}", 10.0/3.0);
The output from this statement is shown here:
Here is 10/3: 3.33
In this example, the template is #.##, which tells WriteLine( ) to display two decimal places.
It is important to understand, however, that WriteLine( ) will display more than one digit to
the left of the decimal point if necessary so as not to misrepresent the value.
If you want to display monetary values, use the C format specifier. For example,
decimal balance;
balance = 12323.09m;
Console.WriteLine("Current balance is {0:C}", balance);
The output from this sequence is shown here (in U.S. dollar format):
Current balance is $12,323.09
Talk to Mars
At its closest point to Earth, Mars is approximately 34 million miles away. Assuming there
is someone on Mars you want to talk with, what is the delay between the time a radio signal
leaves Earth and the time it arrives on Mars? This program supplies the answer. Recall that light
travels approximately 186,000 miles per second. Thus, to compute the delay, you will need to
divide the distance by the speed of light. Display the delay in terms of seconds and minutes.

zied toumi
10-20-2013, 00:23
Step by Step
1. Create a new file called Mars.cs.
2. To compute the delay, you will need to use floating-point values. Why? Because the time
interval will have a fractional component. Here are the variables used by the program:
double distance;
double lightspeed;
double delay;
double delay_in_min;
3. Give distance and lightspeed these values:
distance = 34000000; // 34,000,000 miles
lightspeed = 186000; // 186,000 miles per second
4. To compute the delay, divide distance by lightspeed. This yields the delay in seconds.
Assign this value to delay and display the results. These steps are shown here:
delay = distance / lightspeed;
Console.WriteLine("Time delay when talking to Mars: " +
delay + " seconds.");
5. Divide the number of seconds in delay by 60 to obtain the delay in minutes; display that
result using these lines of code:
delay_in_min = delay / 60;
Console.WriteLine("This is " + delay_in_min +
" minutes.");
Here is the entire Mars.cs program listing:
// Talk to Mars
using System;
class Mars {
static void Main() {
double distance;
double lightspeed;
double delay;
double delay_in_min;
distance = 34000000; // 34,000,000 miles
lightspeed = 186000; // 186,000 miles per second
delay = distance / lightspeed;

---------- Post added at 09:20 PM ---------- Previous post was at 09:19 PM ----------

Console.WriteLine("Time delay when talking to Mars: " +
delay + " seconds.");
delay_in_min = delay / 60;
Console.WriteLine("This is " + delay_in_min +
" minutes.");
}
}
6. Compile and run the program. The following result is displayed:
Time delay when talking to Mars: 182.795698924731 seconds.
This is 3.04659498207885 minutes.
7. For most people, the program displays too many decimal places. To improve the readability
of the program, substitute the following WriteLine( ) statements for the ones shown in the
program:
Console.WriteLine("Time delay when talking to Mars: {0:#.###} seconds",
delay);
Console.WriteLine("This is about {0:#.###} minutes", delay_in_min);
8. Recompile and run the program. When you do, you will see this output:
Time delay when talking to Mars: 182.796 seconds
This is about 3.047 minutes
Now, only three decimal places are displayed.

---------- Post added at 09:20 PM ---------- Previous post was at 09:20 PM ----------

Literals
In C#, literals refer to fixed values that are represented in their human-readable form. For
example, the number 100 is a literal. For the most part, literals and their usage are so intuitive
that they have been used in one form or another by all the preceding sample programs. Now
the time has come to explain them formally.
C# literals can be of any of the value types. The way each literal is represented depends
upon its type. As explained earlier, character literals are enclosed between single quotes. For
example ‘a’ and ‘%’ are both character literals.
Integer literals are specified as numbers without fractional components. For example,
10 and –100 are integer literals. Floating-point literals require the use of the decimal point
followed by the number’s fractional component. For example, 11.123 is a floating-point literal.
C# also allows you to use scientific notation for floating-point numbers.
Since C# is a strongly typed language, literals, too, have a type. Naturally, this raises the
following question: What is the type of a numeric literal? For example, what is the type of 12,
123987, or 0.23? Fortunately, C# specifies some easy-to-follow rules that answer these questions.

---------- Post added at 09:21 PM ---------- Previous post was at 09:20 PM ----------

First, for integer literals, the type of the literal is the smallest integer type that will hold it,
beginning with int. Thus, an integer literal is of type int, uint, long, or ulong, depending upon
its value. Second, floating-point literals are of type double.
If C#’s default type is not what you want for a literal, you can explicitly specify its type by
including a suffix. To specify a long literal, append an l or an L. For example, 12 is an int, but
12L is a long. To specify an unsigned integer value, append a u or U. Thus, 100 is an int, but
100U is a uint. To specify an unsigned, long integer, use ul or UL. For example, 984375UL is
of type ulong.
To specify a float literal, append an F or f. For example, 10.19F is of type float. Although
redundant, you can specify a double literal by appending a D or d. (As just mentioned,
floating-point literals are double by default.)
To specify a decimal literal, follow its value with an m or M. For example, 9.95M is
a decimal literal.
Although integer literals create an int, uint, long, or ulong value by default, they can
still be assigned to variables of type byte, sbyte, short, or ushort as long as the value being
assigned can be represented by the target type.

---------- Post added at 09:21 PM ---------- Previous post was at 09:21 PM ----------

Hexadecimal Literals
In programming, it is sometimes easier to use a number system based on 16 instead of 10. The
base 16 number system is called hexadecimal and uses the digits 0 through 9, plus the letters
A through F, which stand for 10, 11, 12, 13, 14, and 15. For example, the hexadecimal number
10 is 16 in decimal. Because of the frequency with which hexadecimal numbers are used, C#
allows you to specify integer literals in hexadecimal format. A hexadecimal literal must begin
with 0x (a zero followed by an x). Here are some examples:
count = 0xFF; // 255 in decimal
incr = 0x1a; // 26 in decimal
Character Escape Sequences
Enclosing character constants in single quotes works for most printing characters, but a few
characters, such as the carriage return, pose a special problem when a text editor is used. In
addition, certain other characters, such as single and double quotes, have special meaning
in C#, so you cannot use them directly. For these reasons, C# provides special escape
sequences, shown in Table 2-2. These sequences are used in place of the characters that they
represent.
For example, this assigns ch the tab character:
ch = '\t';
The next example assigns a single quote to ch:
ch = '\'';
Escape Sequence Description
\a Alert (bell)
\b Backspace
\f Form feed
\n Newline (linefeed)
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0 Null
\' Single quote
\" Double quote
\\ Backslash

---------- Post added at 09:22 PM ---------- Previous post was at 09:21 PM ----------

String Literals
C# supports another type of literal: the string. A string literal is a set of characters enclosed by
double quotes. For example,
"this is a test"
is a string. You have seen examples of strings in many of the WriteLine( ) statements in the
preceding sample programs.
In addition to normal characters, a string literal can also contain one or more of the escape
sequences just described. For example, consider the following program. It uses the \n and \t
escape sequences.
Q: I know that C++ allows integer literals to be specified in octal (a number system based
on 8). Does C# allow octal literals?
A: No. C# allows integer literals to be specified only in decimal or hexadecimal form. Octal is
seldom used in today’s modern programming environments.

---------- Post added at 09:23 PM ---------- Previous post was at 09:22 PM ----------

// Demonstrate escape sequences in strings.
using System;
class StrDemo {
static void Main() {
Console.WriteLine("First line\nSecond line");
Console.WriteLine("A\tB\tC");
Console.WriteLine("D\tE\tF");
}
}
The output is shown here:
First line
Second line
A B C
D E F
Notice how the \n escape sequence is used to generate a new line. You don’t need to use
multiple WriteLine( ) statements to get multiline output. Just embed \n within a longer string
at the points at which you want the newlines to occur.
In addition to the form of string literal just described, you can specify a verbatim string
literal. A verbatim string literal begins with an @, which is followed by a quoted string. The
contents of the quoted string are accepted without modification and can span two or more
lines. Thus, you can include newlines, tabs, and so on, but you don’t need to use the escape
sequences. The only exception is that to obtain a double quote ("), you must use two double
quotes in a row (""). Here is a program that demonstrates verbatim string literals:

---------- Post added at 09:23 PM ---------- Previous post was at 09:23 PM ----------

// Demonstrate verbatim literal strings.
using System;
class Verbatim {
static void Main() {
Console.WriteLine(@"This is a verbatim
string literal
that spans several lines.
");
Console.WriteLine(@"Here is some tabbed output:
1 2 3 4
5 6 7 8
");
Console.WriteLine(@"Programmers say, ""I like C#.""");
}
}
The output from this program is shown here:
This is a verbatim
string literal
that spans several lines.
Here is some tabbed output:
1 2 3 4
5 6 7 8
Programmers say, "I like C#."
The important point to notice about the preceding program is that the verbatim string literals
are displayed precisely as they are entered into the program.
The advantage of verbatim string literals is that you can specify output in your program
exactly as it will appear on the screen. However, in the case of multiline strings, the wrapping
will cause the indentation of your program to be obscured. For this reason, the programs in this
book will make only limited use of verbatim string literals. That said, they are still a wonderful
benefit for many formatting situations.

zied toumi
10-20-2013, 00:28
A Closer Look at Variables
Variables were introduced in Chapter 1. As you learned, variables are declared using this form
of statement:
type var-name;
where type is the data type of the variable and var-name is its name. You can declare a variable
of any valid type, including the value types just described. It is important to understand that
the capabilities of the variable are determined by its type. For example, a variable of type
bool cannot be used to store floating-point values. Furthermore, the type of a variable cannot
change during its lifetime. An int variable cannot turn into a char variable, for example.
All variables in C# must be declared. This is necessary because the compiler must know
what type of data a variable contains before it can properly compile any statement that uses the
variable. It also enables C# to perform strict type-checking.
C# defines several different kinds of variables. The kinds that we have been using are
called local variables because they are declared within a method.
Q: Is a string consisting of a single character the same as a character literal? For
example, is “k” the same as ‘k’?
A: No. You must not confuse strings with characters. A character literal represents a single
letter of type char. A string containing only one letter is still a string. Although strings
consist of characters, they are not the same type.
Ask the Expert

---------- Post added at 09:24 PM ---------- Previous post was at 09:24 PM ----------

Initializing a Variable
One way to give a variable a value is through an assignment statement, as you have already
seen. Another way is by giving it an initial value when it is declared. To do this, follow
the variable’s name with an equal sign and the value being assigned. The general form of
initialization is shown here:
type var-name = value;
Here, value is the value that is given to var-name when var-name is created. The value must be
compatible with the specified type.
Here are some examples:
int count = 10; // give count an initial value of 10
char ch = 'X'; // initialize ch with the letter X
float f = 1.2F; // f is initialized with 1.2
When declaring two or more variables of the same type using a comma-separated list, you
can give one or more of those variables an initial value. For example:
int a, b = 8, c = 19, d; // b and c have initializations
In this case, only b and c are initialized.

---------- Post added at 09:25 PM ---------- Previous post was at 09:24 PM ----------

Dynamic Initialization
Although the preceding examples have used only constants as initializers, C# allows variables
to be initialized dynamically, using any expression valid at the point at which the variable is
declared. For example, here is a short program that computes the volume of a cylinder given
the radius of its base and its height:
// Demonstrate dynamic initialization.
using System;
class DynInit {
static void Main() {
double radius = 4, height = 5;
// Dynamically initialize volume.
double volume = 3.1416 * radius * radius * height;
Console.WriteLine("Volume is " + volume);
}
}
Here, three local variables—radius, height, and volume—are declared. The first two, radius and
height, are initialized by constants. However, volume is initialized dynamically to the volume of
the cylinder. The key point here is that the initialization expression can use any element valid at
the point of the initialization, including calls to methods, other variables, or literals.

---------- Post added at 09:26 PM ---------- Previous post was at 09:25 PM ----------

Implicitly Typed Variables
As explained, in C# all variables must be declared. Normally, a declaration includes the type
of the variable, such as int or bool, followed by the name of the variable. However, beginning
with C# 3.0, it is possible to let the compiler determine the type of a variable based on the
value used to initialize it. This is called an implicitly typed variable.
An implicitly typed variable is declared using the keyword var, and it must be initialized.
The compiler uses the type of the initializer to determine the type of the variable. Here is an
example:
var pi = 3.1416;
Because pi is initialized with a floating-point literal (whose type is double by default), the type
of pi is double. Had pi been declared like this:
var pi = 3.1416M;
then pi would have the type decimal instead.
The following program demonstrates implicitly typed variables:
// Demonstrate implicitly typed variables.

---------- Post added at 09:27 PM ---------- Previous post was at 09:26 PM ----------

using System;
class ImpTypedVar {
static void Main() {
// These are implicitly typed variables.
var pi = 3.1416; // pi is a double
var radius = 10; // radius is an int
// Both msg and msg2 are string types.
var msg = "Radius: ";
var msg2 = "Area: ";
// Explicitly declare area as a double.
double area;
Console.WriteLine(msg2 + radius);
area = pi * radius * radius;
Console.WriteLine(msg + area);
Console.WriteLine();
radius = radius + 2;
Console.WriteLine(msg2 + radius);
area = pi * radius * radius;

---------- Post added at 09:28 PM ---------- Previous post was at 09:27 PM ----------

Console.WriteLine(msg + area);
// The following statement will not compile because
// radius is an int and cannot be assigned a floating-
// point value.
// radius = 12.2; // Error!
}
}
The output is shown here:
Area: 10
Radius: 314.16
Area: 12
Radius: 452.3904
It is important to emphasize that an implicitly typed variable is still a strongly typed
variable. Notice this commented-out line in the program:
// radius = 12.2; // Error!
This assignment is invalid because radius is of type int. Thus, it cannot be assigned a floatingpoint
value. The only difference between an implicitly typed variable and a “normal” explicitly
typed variable is how the type is determined. Once that type has been determined, the variable
has a type, and this type is fixed throughout the lifetime of the variable. Thus, the type of
radius cannot be changed during the execution of the program.
Implicitly typed variables were added to C# to handle some special-case situations, the
most important of which relate to LINQ (language-integrated query), which is described later
in this book. For the majority of variables, you should use explicitly typed variables because
they make your code easier to read and easier to understand. Implicitly typed variables should
be used only when necessary. They are not intended to replace normal variable declarations in
general. In essence, use, but don’t abuse, this new C# feature.
One last point: Only one implicitly typed variable can be declared at any one time.
Therefore, the following declaration
var count = 10, max = 20; // Error!
is wrong and won’t compile because it attempts to declare both count and max at the same time.
The Scope and Lifetime of Variables
So far, all of the variables that we have been using are declared at the start of the Main( )
method. However, C# allows a local variable to be declared within any block. As explained in
Chapter 1, a block is begun with an ****ing curly brace and ended with a closing curly brace.
A block defines a scope. Thus, each time you start a new block, you are creating a new scope.
A scope determines what names are visible to other parts of your program. It also determines
the lifetime of local variables.

---------- Post added at 09:28 PM ---------- Previous post was at 09:28 PM ----------

The most important scopes in C# are those defined by a class and those defined by a method. A
discussion of class scope (and variables declared within it) is deferred until later in this book, when
classes are described. For now, we will examine only the scopes defined by or within a method.
The scope defined by a method begins with its ****ing curly brace and ends with its
closing curly brace. However, if that method has parameters, they, too, are included within the
scope defined by the method.
As a general rule, local variables declared inside a scope are not visible to code that is defined
outside that scope. Thus, when you declare a variable within a scope, you are preventing it
from being accessed or modified by code outside the scope. Indeed, the scope rules provide the
foundation for encapsulation.
Scopes can be nested. For example, each time you create a block of code, you are creating
a new, nested scope. When this occurs, the outer scope encloses the inner scope. This means
that local variables declared in the outer scope will be visible to code within the inner scope.
However, the reverse is not true. Local variables declared within the inner scope will not be
visible outside it.
To understand the effect of nested scopes, consider the following program:

ahmedamire
10-20-2013, 00:28
شكرا جزيلا اخي على هذا الكتاب الرائع والذي استفدت منه كثيرا
وارجو ان تكثر من مثل هذا المجذو لتزيدنا علما ومعرفتا

zied toumi
10-20-2013, 00:37
// Demonstrate block scope.
using System;
class ScopeDemo {
static void Main() {
int x; // known to all code within Main()
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
Console.WriteLine("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
Console.WriteLine("x is " + x);
}
}
As the comments indicate, the variable x is declared at the start of Main( )’s scope and is
accessible to all subsequent code within Main( ). Within the if block, y is declared. Since a
block defines a scope, y is visible only to other code within its block. This is why outside of
its block, the line y = 100; is commented out. If you remove the leading comment symbol, a
compile-time error will occur because y is not visible outside of its block. Within the if block,
x can be used because code within a block (that is, a nested scope) has access to variables
declared by an enclosing scope.

---------- Post added at 09:29 PM ---------- Previous post was at 09:29 PM ----------

Within a block, local variables can be declared at any point, but are valid only after they
are declared. Thus, if you define a variable at the start of a method, it is available to all of
the code within that method. Conversely, if you declare a variable at the end of a block, it is
effectively useless, because no code will have access to it.
If a variable declaration includes an initializer, that variable will be reinitialized each time
the block in which it is declared is entered. For example, consider this program:
// Demonstrate the lifetime of a variable.
using System;
class VarInitDemo {
static void Main() {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
Console.WriteLine("y is: " + y); // this always prints -1
y = 100;
Console.WriteLine("y is now: " + y);
}
}
}

---------- Post added at 09:30 PM ---------- Previous post was at 09:29 PM ----------

The output generated by this program is shown here:
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
As you can see, y is always reinitialized to –1 each time the for block is entered. Even though
it is subsequently assigned the value 100, this value is lost.
There is one quirk to C#’s scope rules that may surprise you: Although blocks can be
nested, no variable declared within an inner scope can have the same name as a variable
declared by an enclosing scope. For example, the following program, which tries to declare
two separate variables with the same name, will not compile:
/*
This program attempts to declare a variable
in an inner scope with the same name as one
defined in an outer scope.
*** This program will not compile. ***
*/
using System;

---------- Post added at 09:31 PM ---------- Previous post was at 09:30 PM ----------

class NestVar {
static void Main() {
int count;
for(count = 0; count < 10; count = count+1) {
Console.WriteLine("This is count: " + count);
// Illegal!!! This conflicts with the previous count.
int count;
for(count = 0; count < 2; count++)
Console.WriteLine("This program is in error!");
}
}
}
If you come from a C/C++ background, you know that there is no restriction on the names
that you give variables declared in an inner scope. Thus, in C/C++, the declaration of count
within the block of the outer for loop is completely valid. However, in C/C++, such a declaration
hides the outer count. The designers of C# felt that this type of name hiding could easily lead to
programming errors and disallowed it.

---------- Post added at 09:32 PM ---------- Previous post was at 09:31 PM ----------

Operators
C# provides a rich operator environment. An operator is a symbol that tells the compiler
to perform a specific mathematical or logical manipulation. C# has four general classes of
operators: arithmetic, bitwise, relational, and logical. C# also has several other operators that
handle certain special situations. This chapter will examine the arithmetic, relational, and
logical operators. It also examines the assignment operator. The bitwise and other special
operators are examined later.
Arithmetic Operators
C# defines the following arithmetic operators:
Operator Meaning
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
– – Decrement

---------- Post added at 09:33 PM ---------- Previous post was at 09:32 PM ----------

The operators +, –, *, and / all work in the expected way. These can be applied to any built-in
numeric data type.
Although the actions of the basic arithmetic operators are well known to all readers,
the % warrants some explanation. First, remember that when / is applied to an integer, any
remainder will be truncated; for example, 10/3 will equal 3 in integer division. You can obtain
the remainder of this division by using the modulus operator %. It works in C# the way that
it does in other languages: It yields the remainder of an integer division. For example, 10 % 3
is 1. In C#, the % can be applied to both integer and floating-point types. Thus, 10.0 % 3.0 is
also 1. (This differs from C/C++, which allow modulus operations only on integer types.) The
following program demonstrates the modulus operator:
// Demonstrate the % operator.
using System;
class ModDemo {
static void Main() {
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
Console.WriteLine("Result and remainder of 10 / 3: " +
iresult + " " + irem);
Console.WriteLine("Result and remainder of 10.0 / 3.0: " +
dresult + " " + drem);
}
}

---------- Post added at 09:34 PM ---------- Previous post was at 09:33 PM ----------

The output from the program is shown here:
Result and remainder of 10 / 3: 3 1
Result and remainder of 10.0 / 3.0: 3.33333333333333 1
As you can see, the % yields a remainder of 1 for both integer and floating-point operations.
Increment and Decrement
Introduced in Chapter 1, the ++ and the – – are the increment and decrement operators, respectively.
As you will see, they have some special properties that make them quite interesting. Let’s begin by
reviewing precisely what the increment and decrement operators do.
The increment operator adds 1 to its operand, and the decrement operator subtracts 1.
Therefore:
x = x + 1;

---------- Post added at 09:35 PM ---------- Previous post was at 09:34 PM ----------

can be written as
++x; // prefix form
or as
x++; // postfix form
In the foregoing example, there is no difference whether the increment is applied as a prefix
or a postfix. However, when an increment or decrement is used as part of a larger expression,
there is an important difference. In this case, when the operator precedes its operand, the result
of the operation is the value of the operand after the increment or decrement. If the operator
follows its operand, the result of the operation is the value of the operand before the increment or
decrement. Consider the following:
x = 10;
y = ++x;
In this case, y will be set to 11. This is because x is first incremented and then its value is obtained.
However, if the code is written as
x = 10;
y = x++;
then y will be set to 10. In this case, the value of x is first obtained, x is incremented, and then
the original value of x is returned. In both cases, x is still set to 11; the difference is what is
returned by the operator.
Here is a bit more complicated example:
x = 10;
y = 2;
z = x++ - (x * y);

---------- Post added at 09:36 PM ---------- Previous post was at 09:35 PM ----------

This gives z the value –12. The reason is that when x++ is evaluated, it sets x to 11, but yields
the value 10 (x’s original value). This means that z is assigned 10 – (11 * 2), which is –12.
However, written like this:
z = ++x - (x * y);
The result is –11. This is because the x is first incremented and then its value is obtained. Thus,
z is assigned 11 – (11 * 2), which is –11. As this example hints at, there can be significant
advantages in being able to control when the increment or decrement operation takes place.
Relational and Logical Operators
In the terms relational operator and logical operator, relational refers to the relationships that
values can have with one another, and logical refers to the ways in which true and false values
can be connected together. Since the relational operators produce true or false results, they
often work with the logical operators. For this reason, they will be discussed together here.
The relational operators are shown here:

---------- Post added at 09:37 PM ---------- Previous post was at 09:36 PM ----------

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
The logical operators are shown next:
Operator Meaning
& AND
| OR
^ XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! NOT
The outcome of the relational and logical operators is a bool value.
In general, objects can be compared for equality or inequality using == and !=. However,
the comparison operators, <, >, <=, or >=, can be applied only to those types that support an
ordering relationship. Therefore, all of the relational operators can be applied to all numeric
types. However, values of type bool can only be compared for equality or inequality, since the
true and false values are not ordered. For example, true > false has no meaning in C#.

zied toumi
10-20-2013, 00:44
As the table shows, the outcome of an exclusive OR operation is true only when exactly one
operand is true.
Here is a program that demonstrates several of the relational and logical operators:
// Demonstrate the relational and logical operators.
using System;
class RelLogOps {
static void Main() {
int i, j;
bool b1, b2;
i = 10;
j = 11;
if(i < j) Console.WriteLine("i < j");
if(i <= j) Console.WriteLine("i <= j");
if(i != j) Console.WriteLine("i != j");
if(i == j) Console.WriteLine("this won't execute");
if(i >= j) Console.WriteLine("this won't execute");
if(i > j) Console.WriteLine("this won't execute");
b1 = true;
b2 = false;
if(b1 & b2) Console.WriteLine("this won't execute");
if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true");
if(b1 | b2) Console.WriteLine("b1 | b2 is true");
if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true");
}
}
The output from the program is shown here:
i < j
i <= j
i != j
!(b1 & b2) is true
b1 | b2 is true
b1 ^ b2 is true

---------- Post added at 09:39 PM ---------- Previous post was at 09:38 PM ----------

Short-Circuit Logical Operators
C# supplies special short-circuit versions of its AND and OR logical operators that can be
used to produce more efficient code. To understand why, consider the following. In an AND
operation, if the first operand is false, the outcome is false, no matter what value the second
operand has. In an OR operation, if the first operand is true, the outcome of the operation is
true, no matter what the value of the second operand. Thus, in these two cases, there is no need
to evaluate the second operand. By not evaluating the second operand, time is saved and more
efficient code is produced.
The short-circuit AND operator is &&, and the short-circuit OR operator is ||. As described
earlier, their normal counterparts are & and |. The only difference between the normal and
short-circuit versions is that the normal operands will always evaluate each operand, but shortcircuit
versions will evaluate the second operand only when necessary.
Here is a program that demonstrates the short-circuit AND operator. The program
determines if the value in d is a factor of n. It does this by performing a modulus operation.
If the remainder of n / d is zero, then d is a factor. However, since the modulus operation
involves a division, the short-circuit form of the AND is used to prevent a divide-by-zero error.
// Demonstrate the short-circuit operators.
using System;
class SCops {
static void Main() {
int n, d;
n = 10;
d = 2;
// Here, d is 2, so the modulus operation takes place.
if(d != 0 && (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
d = 0; // now, set d to zero

---------- Post added at 09:39 PM ---------- Previous post was at 09:39 PM ----------

// Since d is zero, the second operand is not evaluated.
if(d != 0 && (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
// Now, try same thing without short-circuit operator.
// This will cause a divide-by-zero error.
if(d != 0 & (n % d) == 0)
Console.WriteLine(d + " is a factor of " + n);
}
}
To prevent a divide-by-zero error, the if statement first checks to see if d is equal to zero.
If it is, the short-circuit AND stops at that point and does not perform the modulus division.

---------- Post added at 09:40 PM ---------- Previous post was at 09:39 PM ----------

Thus, in the first test, d is 2 and the modulus operation is performed. Next, d is set to zero.
This causes the second test to fail, and the modulus operation is skipped, avoiding a divideby-
zero error. Finally, the normal AND operator is tried. This causes both operands to be
evaluated, which leads to a runtime error when the division-by-zero occurs.
One other point: The short-circuit AND is also known as the conditional AND, and the
short-circuit OR is also called the conditional OR.
Display a Truth Table for the Logical Operators
Here you will create a program that displays the truth table for C#’s logical operators. This
program makes use of several features covered in this chapter, including one of C#’s character
escape sequences and the logical operators. It also illustrates the differences in the precedence
between the arithmetic + operator and the logical operators.

---------- Post added at 09:41 PM ---------- Previous post was at 09:40 PM ----------

Step by Step
1. Create a new file called LogicalOpTable.cs.
2. To ensure that the columns line up, use the \t escape sequence to embed tabs into each
output string. For example, this WriteLine( ) statement displays the header for the table:
Console.WriteLine("P\tQ\tAND\tOR\tXOR\tNOT");
3. For each subsequent line in the table, use tabs to properly position the outcome of each
operation under its proper heading.
4. Here is the entire LogicalOpTable.cs program listing. Enter it at this time.
// Print a truth table for the logical operators.
using System;
class LogicalOpTable {
static void Main() {
bool p, q;
Console.WriteLine("P\tQ\tAND\tOR\tXOR\tNOT");
p = true; q = true;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = true; q = false;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = false; q = true;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
p = false; q = false;
Console.Write(p + "\t" + q +"\t");
Console.Write((p&q) + "\t" + (p|q) + "\t");
Console.WriteLine((p^q) + "\t" + (!p));
}
}

---------- Post added at 09:41 PM ---------- Previous post was at 09:41 PM ----------

6. Notice the parentheses surrounding the logical operations inside the Write( ) and WriteLine( )
statements. They are necessary because of the precedence of C#’s operators. The + operator
is higher than the logical operators.
7. On your own, try modifying the program so that it uses and displays 1’s and 0’s, rather than
true and false.
Q: Since the short-circuit operators are, in some cases, more efficient than their normal
counterparts, why does C# still offer the normal AND and OR operators?
A: In some cases, you will want both operands of an AND or OR operation to be evaluated
because of the side effects produced. Consider the following:
// Side effects can be important.
using System;
class SideEffects {
static void Main() {
int i;
i = 0;
// Here, i is incremented even though the if statement fails.

---------- Post added at 09:42 PM ---------- Previous post was at 09:41 PM ----------

if(false & (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // displays 1
// In this case, i is not incremented because the short-circuit
// operator skips the increment.
if(false && (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // still 1 !!
}
}
As the comments indicate, in the first if statement, i is incremented whether the if
succeeds or not. However, when the short-circuit operator is used, the variable i is not
incremented when the first operand is false. The lesson here is that if your code expects the
right-hand operand of an AND or OR operation to be evaluated, you must use the normal
forms of these operations.

---------- Post added at 09:43 PM ---------- Previous post was at 09:42 PM ----------

The Assignment Operator
You have been using the assignment operator since Chapter 1. Now it is time to take a formal
look at it. The assignment operator is the single equal sign, =. The assignment operator works
in C# much as it does in other computer languages. It has this general form:
var = expression;
Here, the type of var must be compatible with the type of expression.
The assignment operator does have one interesting attribute with which you may not be
familiar: It allows you to create a chain of assignments. For example, consider this fragment:
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
This fragment sets the variables x, y, and z to 100 using a single statement. This works because
the = is an operator that yields the assigned value. Thus, the value of z = 100 is 100, which is
then assigned to y, which, in turn, is assigned to x. Using a “chain of assignment” is an easy
way to set a group of variables to a common value.
Compound Assignments
C# provides special compound assignment operators that simplify the coding of certain
assignment statements. Let’s begin with an example. The assignment statement shown here:
x = x + 10;

---------- Post added at 09:44 PM ---------- Previous post was at 09:43 PM ----------

can be written using a compound assignment, such as
x += 10;
The operator pair += tells the compiler to assign to x the value of x plus 10.
Here is another example. The statement
x = x - 100;
is the same as
x -= 100;
Both statements assign to x the value of x minus 100.
There are compound assignment operators for many of the binary operators (that is, those
that require two operands). The general form of the shorthand is
variable op = expression;
Thus, the arithmetic and logical assignment operators are

zied toumi
10-20-2013, 00:45
+= –= *= /=
%= &= |= ^=
Because the compound assignment statements are shorter than their noncompound
equivalents, the compound assignment operators are also sometimes called the shorthand
assignment operators.
The compound assignment operators provide two benefits. First, they are more compact
than their “longhand” equivalents. Second, they can result in more efficient executable code
(because the left-hand operand is evaluated only once). For these reasons, you will often see
the compound assignment operators used in professionally written C# programs.
Type Conversion in Assignments
In programming, it is common to assign a value of one type to a variable of another type.
For example, you might want to assign an int value to a float variable, as shown here:
int i;
float f;
i = 10;
f = i; // assign an int to a float
When compatible types are mixed in an assignment, the value of the right side is automatically
converted to the type of the left side. Thus, in the preceding fragment, the value in i is converted
into a float and then assigned to f. However, because of C#’s strict type-checking, not all types
are compatible, and thus, not all type conversions are implicitly allowed. For example, bool and
int are not compatible.

zied toumi
10-20-2013, 00:47
When one type of data is assigned to another type of variable, an implicit type conversion
will take place automatically, if:
● The two types are compatible.
● The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. For example, the int
type is always large enough to hold all valid byte values, and both int and byte are integer
types, so an implicit conversion can be applied.
For widening conversions, the numeric types, including integer and floating-point types,
are compatible with each other. For example, the following program is perfectly valid, since
long to double is a widening conversion that is automatically performed:
// Demonstrate implicit conversion from long to double.
using System;
class LtoD {
static void Main() {
long L;
double D;
L = 100123285L;
D = L;
Console.WriteLine("L and D: " + L + " " + D);
}
}
Although there is an implicit conversion from long to double, there is no implicit
conversion from double to long, since this is not a widening conversion. Thus, the following
version of the preceding program is invalid:
// *** This program will not compile. ***
using System;
class LtoD {
static void Main() {
long L;
double D;
D = 100123285.0;
L = D; // Illegal!!!
Console.WriteLine("L and D: " + L + " " + D);
}
}
In addition to the restrictions just described, there are no implicit conversions between
decimal and float or double, or from the numeric types to char or bool. Also, char and bool
are not compatible with each other.
Casting Incompatible Types
Although the implicit type conversions are helpful, they will not fulfill all programming needs
because they apply only to widening conversions between compatible types. For all other
cases, you must employ a cast. A cast is an instruction to the compiler to convert an expression
into a specified type. Thus, it requests an explicit type conversion. A cast has this general form:
(target-type) expression
Here, target-type specifies the desired type to convert the specified expression to. For example,
if you want the type of the expression x/y to be int, you can write
double x, y;
// ...
(int) (x / y)
Here, even though x and y are of type double, the cast converts the outcome of the expression
to int. The parentheses surrounding x / y are necessary. Otherwise, the cast to int would apply
only to the x, and not to the outcome of the division. The cast is necessary here because there
is no implicit conversion from double to int.
When a cast involves a narrowing conversion, information might be lost. For example,
when casting a long into an int, information will be lost if the long’s value is greater than the
range of an int because its high-order bits are removed. When a floating-point value is cast to
an integer type, the fractional component will also be lost due to truncation. For example, if
the value 1.23 is assigned to an integer, the resulting value will simply be 1. The 0.23 is lost.
The following program demonstrates some type conversions that require casts:
// Demonstrate casting.
using System;
class CastDemo {
static void Main() {
double x, y;
byte b;
int i;
char ch;
x = 10.0;
y = 3.0;
i = (int) (x / y);
Console.WriteLine("Integer outcome of x / y: " + i);
i = 100;
b = (byte) i;
Console.WriteLine("Value of b: " + b);
i = 257;
b = (byte) i;
Console.WriteLine("Value of b: " + b);
b = 88; // ASCII code for X
ch = (char) b;
Console.WriteLine("ch: " + ch);
}
}
The output from the program is shown here:
Integer outcome of x / y: 3
Value of b: 100
Value of b: 1
ch: X
In the program, the cast of (x / y) to int results in the truncation of the fractional component
and information is lost. Next, no loss of information occurs when b is assigned the value
100 because a byte can hold the value 100. However, when the attempt is made to assign
b the value 257, information loss occurs because 257 exceeds a byte’s range. This results
in b having the value 1 because only the 1 bit is set in the low-order 8 bits of the binary
representation of 257. Finally, no information is lost, but a cast is needed when assigning a
byte value to a char.
Operator Precedence
Table 2-3 shows the order of precedence for all C# operators, from highest to lowest. This
table includes several operators that will be discussed later in this book.
Type Conversion in Expressions
Within an expression, it is possible to mix two or more different types of data, as long as they
are compatible with each other. For example, you can mix short and long within an expression
because they are both numeric types. When different types of data are mixed within an
expression, they are converted to the same type on an operation-by-operation basis.
The conversions are accomplished through the use of C#’s type promotion rules. Here is
the algorithm that the rules define for binary operations:
IF one operand is decimal, THEN the other operand is promoted to decimal
(unless it is of type float or double, in which case an error results).
ELSE IF one of the operands is double, the second is promoted to double.
Highest
() [] . ++(postfix) --(postfix)
checked new sizeof typeof unchecked
! ~ (cast) +(unary) -(unary) ++(prefix) --(prefix)
* / %
+ -
<< >>
< > <= >= is
== !=
&
^
|
&&
||
??
?:
= op= =>
ELSE IF one operand is a float operand, the second is promoted to float.
ELSE IF one operand is a ulong, the second is promoted to ulong (unless it is
of type sbyte, short, int, or long, in which case an error results).
ELSE IF one operand is a long, the second is promoted to long.
ELSE IF one operand is a uint and the second is of type sbyte, short, or int,
both are promoted to long.
ELSE IF one operand is a uint, the second is promoted to uint.
ELSE both operands are promoted to int.
There are a couple of important points to be made about the type promotion rules. First,
not all types can be mixed in an expression. Specifically, there is no implicit conversion from
float or double to decimal, and it is not possible to mix ulong with any signed integer type. To
mix these types requires the use of an explicit cast.
Second, *** special attention to the last rule. It states that if none of the preceding rules
applies, then all other operands are promoted to int. Therefore, in an expression, all char,
sbyte, byte, ushort, and short values are promoted to int for the purposes of calculation. This
is called integer promotion. It also means that the outcome of all arithmetic operations will be
no smaller than int.
It is important to understand that type promotions apply to the values operated upon only
when an expression is evaluated. For example, if the value of a byte variable is promoted to
int inside an expression, outside the expression, the variable is still a byte. Type promotion
only affects the evaluation of an expression.
Type promotion can, however, lead to somewhat unexpected results. For example, when an
arithmetic operation involves two byte values, the following sequence occurs: First, the byte
operands are promoted to int. Then the operation takes place, yielding an int result. Thus, the
outcome of an operation involving two byte values will be an int. This is not what you might
intuitively expect. Consider the following program:
// A type promotion surprise!
using System;
class PromDemo {
static void Main() {
byte b;
int i;
b = 10;
i = b * b; // OK, no cast needed
b = 10;
b = (byte) (b * b); // cast needed!!
Console.WriteLine("i and b: " + i + " " + b);
}
}
Somewhat counterintuitively, no cast is needed when assigning b * b to i, because b is
promoted to int when the expression is evaluated. Thus, the result type of b * b is int. However,
when you try to assign b * b to b, you do need a cast—back to byte! Keep this in mind if you
get unexpected type-incompatibility error messages on expressions that would otherwise seem
perfectly okay.
This same sort of situation also occurs when performing operations on chars. For example,
in the following fragment, the cast back to char is needed because of the promotion of ch1 and
ch2 to int within the expression:
char ch1 = 'a', ch2 = 'b';
ch1 = (char) (ch1 + ch2);
Without the cast, the result of adding ch1 to ch2 would be int, which can’t be assigned to a char.
Casts are not only useful when converting between types in an assignment. They can be
used within an expression. For example, consider the following program. It uses a cast to
double to obtain a fractional component from an otherwise integer division.
// Using a cast.
using System;
class UseCast {
static void Main() {
int i;
for(i = 1; i < 5; i++) {
Console.WriteLine(i + " / 3: " + i / 3);
Console.WriteLine(i + " / 3 with fractions: {0:#.##}",
(double) i / 3);
Console.WriteLine();
}
}
}
In the expression:
(double) i / 3
the cast to double causes i to be converted to double, which ensures that the fractional
component of the division by 3 is preserved. The output from the program is shown here:
1 / 3: 0
1 / 3 with fractions: .33
2 / 3: 0
2 / 3 with fractions: .67
3 / 3: 1
3 / 3 with fractions: 1
4 / 3: 1
4 / 3 with fractions: 1.33
Q: Do type promotions occur when a unary operation, such as the unary –, takes place?
A: Yes. For the unary operations, operands smaller than int (byte, sbyte, short, and ushort)
are promoted to int. Also, a char operand is converted to int. Furthermore, if a uint value
is negated, it is promoted to long.

zied toumi
10-20-2013, 00:55
Spacing and Parentheses
An expression in C# can have tabs and spaces in it to make it more readable. For example, the
following two expressions are the same, but the second is easier to read:
x=10/y*(127-x);
x = 10 / y * (127 - x);
Parentheses can be used to group subexpressions, thereby effectively increasing the
precedence of the operations contained within them, just like in algebra. Use of redundant
or additional parentheses will not cause errors or slow down the execution of the expression.
You are encouraged to use parentheses to make clear how an expression is evaluated, both for
yourself and for others who may have to maintain your program later. For example, which of
the following two expressions is easier to read?
x = y/3-34*temp+127;
x = (y/3) - (34*temp) + 127;

---------- Post added at 09:49 PM ---------- Previous post was at 09:48 PM ----------

Compute the Regular ***ments on a Loan
As mentioned earlier, the decimal type is especially well suited for calculations that involve
money. This program demonstrates its use in this capacity. The program computes the regular
***ments on a loan, such as a car loan. Given the principal, the length of time, number of
***ments per year, and the interest rate, the program will compute the ***ment. Since this
is a financial calculation, it makes sense to use the decimal type to represent the data. This
program also demonstrates casting and another of C#’s library methods.
To compute the ***ments, you will use the following formula:
IntRate * (Principal / ***PerYear)
***ment =
1 – ((IntRate / ***PerYear) + 1) – ***PerYear * NumYears
where IntRate specifies the interest rate, Principal contains the starting balance, ***PerYear
specifies the number of ***ments per year, and NumYears specifies the length of the loan in years.
Notice that in the denominator of the formula, you must raise one value to the power of
another. To do this, you will use the C# math method Math.Pow( ). Here is how you will
call it:
result = Math.Pow(base, exp);
Pow( ) returns the value of base raised to the exp power. The arguments to Pow( ) must be of
type double, and it returns a value of type double. This means that you will need to use a cast
to convert between double and decimal.

---------- Post added at 09:49 PM ---------- Previous post was at 09:49 PM ----------

Step by Step
1. Create a new file called Reg***.cs.
2. Here are the variables that will be used by the program:
decimal Principal; // original principal
decimal IntRate; // interest rate as a decimal, such as 0.075
decimal ***PerYear; // number of ***ments per year
decimal NumYears; // number of years
decimal ***ment; // the regular ***ment
decimal numer, denom; // temporary work variables
double b, e; // base and exponent for call to Pow()

---------- Post added at 09:50 PM ---------- Previous post was at 09:49 PM ----------

Since most of the calculation will be done using the decimal data type, most of the
variables are of type decimal.
Notice how each variable declaration is followed by a comment that describes its use. This
helps anyone reading your program understand the purpose of each variable. Although we
won’t include such detailed comments for most of the short programs in this book, it is a
good practice to follow as your programs become longer and more complicated.

---------- Post added at 09:50 PM ---------- Previous post was at 09:50 PM ----------

3. Add the following lines of code, which specify the loan information. In this case, the
principal is $10,000, the interest rate is 7.5 percent, the number of ***ments per year is 12,
and the length of the loan is 5 years.
Principal = 10000.00m;
IntRate = 0.075m;
***PerYear = 12.0m;
NumYears = 5.0m;

---------- Post added at 09:51 PM ---------- Previous post was at 09:50 PM ----------

4. Add the lines that perform the financial calculation:
numer = IntRate * Principal / ***PerYear;
e = (double) -(***PerYear * NumYears);
b = (double) (IntRate / ***PerYear) + 1;
denom = 1 - (decimal) Math.Pow(b, e);
***ment = numer / denom;
Notice how casts must be used to pass values to Pow( ) and to convert the return value.
Remember, there are no implicit conversions between decimal and double in C#.
5. Finish the program by outputting the regular ***ment, as shown here:
Console.WriteLine("***ment is {0:C}", ***ment);
6. Here is the entire Reg***.cs program listing:
// Compute the regular ***ments for a loan.

---------- Post added at 09:51 PM ---------- Previous post was at 09:51 PM ----------

using System;
class Reg*** {
static void Main() {
decimal Principal; // original principal
decimal IntRate; // interest rate as a decimal, such as 0.075
decimal ***PerYear; // number of ***ments per year
decimal NumYears; // number of years
decimal ***ment; // the regular ***ment
decimal numer, denom; // temporary work variables
double b, e; // base and exponent for call to Pow()
Principal = 10000.00m;
IntRate = 0.075m;
***PerYear = 12.0m;
NumYears = 5.0m;
numer = IntRate * Principal / ***PerYear;
e = (double) -(***PerYear * NumYears);
b = (double) (IntRate / ***PerYear) + 1;
denom = 1 - (decimal) Math.Pow(b, e);
***ment = numer / denom;
Console.WriteLine("***ment is {0:C}", ***ment);
}
}
Here is the output from the program:
***ment is $200.38
Before moving on, you migh

---------- Post added at 09:52 PM ---------- Previous post was at 09:51 PM ----------

want to try having the program compute the regular
***ments for differing amounts, periods, and interest rates.
Chapter 2 Self Test
1. Why does C# strictly specify the range and behavior of its simple types?
2. What is C#’s character type, and how does it differ from the character type used by some
other programming languages?
3. A bool value can have any value you like because any nonzero value is true. True or false?

---------- Post added at 09:52 PM ---------- Previous post was at 09:52 PM ----------

4. Given this output:
One
Two
Three
Use a single string and escape sequences to show the WriteLine( ) statement that produced it.
5. What is wrong with this fragment?
for(i = 0; i < 10; i++) {
int sum;
sum = sum + i;
}
Console.WriteLine("Sum is: " + sum);
6. Explain the difference between the prefix and postfix forms of the increment operator.
7. Show how a short-circuit AND can be used to prevent a divide-by-zero error.
8. In an expression, what type are byte and short promoted to?
9. Which of the following types cannot be mixed in an expression with a decimal value?
A. float
B. int
C. uint
D. byte
10. In general, when is a cast needed?
11. Write a program that finds all of the prime numbers between 2 and 100.
12. On your own, rewrite the truth table program in Try This: Display a Truth Table for the
Logical Operators so that it uses verbatim string literals with embedded tab and newline
characters rather escape sequences.

---------- Post added at 09:53 PM ---------- Previous post was at 09:52 PM ----------

Chapter 3
Program Control
Statements

---------- Post added at 09:54 PM ---------- Previous post was at 09:53 PM ----------

Key Skills & Concepts
● Input characters from the keyboard
● if and for
● switch
● while
● do-while
● break
● continue
● goto
This chapter describes the statements that control a program’s flow of execution. There are
three categories of program control statements: selection statements, which include if and
switch; iteration statements, which include the for, while, do-while, and foreach loops; and
jump statements, which include break, continue, goto, return, and throw. Except for return,
foreach, and throw, which are discussed later in this book, the remaining control statements
are examined in detail here.

---------- Post added at 09:55 PM ---------- Previous post was at 09:54 PM ----------

Inputting Characters from the Keyboard
Before examining C#’s control statements, we will make a short digression that will allow you
to begin writing interactive programs. Up to this point, the sample programs in this book have
displayed information to the user, but they have not received information from the user. Thus,
you have been using console output, but not console (that is, keyboard) input. Here, you will
begin to use input by reading characters that are typed at the keyboard.
To read a character from the keyboard, call Console.Read( ). This method waits until the
user presses a key and then returns the key. The character is returned as an integer, so it must
be cast to char to assign it to a char variable. By default, console input is line-buffered, so you
must press ENTER before any character that you type will be sent to your program. Here is a
program that reads a character from the keyboard:
// Read a character from the keyboard.
using System;
class KbIn {
static void Main() {
char ch;

zied toumi
10-20-2013, 01:00
Console.Write("Press a key followed by ENTER: ");
// Read a key from the keyboard.
ch = (char) Console.Read();
Console.WriteLine("Your key is: " + ch);
}
}
Here is a sample run:
Press a key followed by ENTER: t
Your key is: t
The fact that Read( ) is line-buffered is a source of annoyance at times. When you press
ENTER, a carriage-return, linefeed sequence is entered into the input stream. Furthermore, these
characters are left pending in the input buffer until you read them. Thus, for some applications,
you may need to remove them (by reading them) before the next input operation. You will see
an example of this later in this chapter.
The if Statement
Chapter 1 introduced the if statement. It is examined in detail here. The complete form of the
if statement is
if(condition) statement;
else statement;

ismail1992
10-20-2013, 01:05
سلام عليكم ورحمة الله تعالى وبركاتة شركرا على هدا الموضوع مفيد

zied toumi
10-20-2013, 02:17
where the targets of the if and else are single statements. The else clause is optional. Because
a block of statements can be used wherever a single statement is legal, the targets of both the
if and else can be blocks of statements. Therefore, the general form of the if using blocks of
statements is
if(condition)
{
statement sequence
}
else
{
statement sequence
}
If the conditional expression is true, the target of the if will be executed; otherwise, if it
exists, the target of the else will be executed. At no time will both of them be executed. The
conditional expression controlling the if must produce a bool result.

---------- Post added at 10:59 PM ---------- Previous post was at 10:58 PM ----------

To demonstrate the if, we will evolve a simple computerized guessing game that would
be suitable for small children. In the first version of the game, the program asks the player for
a letter between A and Z. If the player presses the right letter on the keyboard, the program
responds by printing the message ** Right **. The program is shown here:
// Guess the letter game.
using System;
class Guess {
static void Main() {
char ch, answer = 'K';
Console.WriteLine("I'm thinking of a letter between A and Z.");
Console.Write("Can you guess it: ");
ch = (char) Console.Read(); // get the user's guess
if(ch == answer) Console.WriteLine("** Right **");
}
}

---------- Post added at 10:59 PM ---------- Previous post was at 10:59 PM ----------

This program prompts the player and then reads a character from the keyboard. Using an
if statement, it then checks that character against the answer, which is K in this case. If K was
entered, the message is displayed. When you try this program, remember that the K must be
entered in uppercase.
Taking the guessing game further, the next version uses the else to print a message when
the wrong letter is picked.
// Guess the letter game, 2nd version.
using System;
class Guess2 {
static void Main() {
char ch, answer = 'K';
Console.WriteLine("I'm thinking of a letter between A and Z.");
Console.Write("Can you guess it: ");
ch = (char) Console.Read(); // get the user's guess
if(ch == answer) Console.WriteLine("** Right **");
else Console.WriteLine("...Sorry, you're wrong.");
}
}

---------- Post added at 11:00 PM ---------- Previous post was at 10:59 PM ----------

Nested ifs
A nested if is an if statement that is the target of another if or else. Nested ifs are very common
in programming. The main thing to remember about nested ifs in C# is that an else clause is
always associated with the nearest if statement that is within the same block as the else and not
already associated with an else. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d;
else a = c; // this else refers to if(k > 100)
}
else a = d; // this else refers to if(i == 10)
As the comments indicate, the final else is not associated with if(j<20), because it is not in the
same block (even though it is the nearest if without an else). Rather, the final else is associated
with if(i==10). The inner else refers to if(k>100), because it is the closest if within the same block.
You can use a nested if to add a further improvement to the guessing game. This addition
provides the player with feedback about a wrong guess.
// Guess the letter game, 3rd version.

---------- Post added at 11:02 PM ---------- Previous post was at 11:00 PM ----------

using System;
class Guess3 {
static void Main() {
char ch, answer = 'K';
Console.WriteLine("I'm thinking of a letter between A and Z.");
Console.Write("Can you guess it: ");
ch = (char) Console.Read(); // get the user's guess
if(ch == answer) Console.WriteLine("** Right **");
else {
Console.Write("...Sorry, you're ");
// A nested if.
if(ch < answer) Console.WriteLine("too low");
else Console.WriteLine("too high");
}
}
}
A sample run is shown here:
I'm thinking of a letter between A and Z.
Can you guess it: Z
...Sorry, you're too high

---------- Post added at 11:03 PM ---------- Previous post was at 11:02 PM ----------

The if-else-if Ladder
A common programming construct that is based upon the nested if is the if-else-if ladder. It
looks like this:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;
The conditional expressions are evaluated from the top down. As soon as a true condition is
found, the statement associated with it is executed, and the rest of the ladder is bypassed. If
none of the conditions are true, the final else clause will be executed. The final else often acts
as a default condition; that is, if all other conditional tests fail, the last else clause is executed.
If there is no final else and all other conditions are false, no action will take place.
The following program demonstrates the if-else-if ladder:

---------- Post added at 11:03 PM ---------- Previous post was at 11:03 PM ----------

// Demonstrate an if-else-if ladder.
using System;
class Ladder {
static void Main() {
int x;
for(x=0; x<6; x++) {
if(x==1)
Console.WriteLine("x is one");
else if(x==2)
Console.WriteLine("x is two");
else if(x==3)
Console.WriteLine("x is three");
else if(x==4)
Console.WriteLine("x is four");
else
Console.WriteLine("x is not between 1 and 4");
}
}
}

---------- Post added at 11:12 PM ---------- Previous post was at 11:03 PM ----------

The program produces the following output:
x is not between 1 and 4
x is one
x is two
x is three
x is four
x is not between 1 and 4
As you can see, the final else is executed only if none of the preceding if statements succeed.
The switch Statement
The second of C#’s selection statements is the switch. The switch provides for a multiway
branch. Thus, it enables a program to select among several alternatives. Although a series of
nested if statements can perform multiway tests, for many situations, the switch is a more
efficient approach. It works like this: The value of an expression is successively tested against
a list of constants. When a match is found, the statement sequence associated with that match
is executed. The general form of the switch statement is
switch(expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}

---------- Post added at 11:12 PM ---------- Previous post was at 11:12 PM ----------

The switch expression must be an integral type, such as char, byte, short, or int; an
enumeration type; or type string. (Enumerations and the string type are described later in
this book.) Thus, floating-point expressions, for example, are not allowed. Frequently, the
expression controlling the switch is simply a variable. The case constants must be of a type
compatible with the expression. No two case constants in the same switch can have identical
values.

---------- Post added at 11:16 PM ---------- Previous post was at 11:12 PM ----------

The default sequence is executed if no case constant matches the expression. The default
is optional; if it is not present, no action takes place if all matches fail. When a match is found,
the statements associated with that case are executed until the break is encountered.
The following program demonstrates the switch:
// Demonstrate the switch.
using System;
class SwitchDemo {
static void Main() {
int i;
for(i=0; i < 10; i++)
switch(i) {
case 0:
Console.WriteLine("i is zero");
break;
case 1:
Console.WriteLine("i is one");
break;
case 2:
Console.WriteLine("i is two");
break;
case 3:
Console.WriteLine("i is three");
break;
case 4:
Console.WriteLine("i is four");
break;
default:
Console.WriteLine("i is five or more");
break;
}
}
}
The output produced by this program is shown here:
i is zero
i is one
i is two
i is three
i is four
i is five or more
i is five or more
i is five or more
i is five or more
i is

---------- Post added at 11:16 PM ---------- Previous post was at 11:16 PM ----------

five or more
As you can see, each time through the loop, the statements associated with the case constant
that matches i are executed. All others are bypassed. When i is five or greater, no case
constants match, so the statements associated with the default case are executed.
In the preceding example, the switch was controlled by an int variable. As explained, you
can control a switch with any integral type, including char. Here is an example that uses a
char expression and char case constants:
// Use a char to control the switch.
using System;
class SwitchDemo2 {
static void Main() {
char ch;
for(ch='A'; ch <= 'E'; ch++)
switch(ch) {
case 'A':
Console.WriteLine("ch is A");
break;
case 'B':
Console.WriteLine("ch is B");
break;
case 'C':
Console.WriteLine("ch is C");
break;
case 'D':
Console.WriteLine("ch is D");
break;
case 'E':
Console.WriteLine("ch is E");
break;
}
}
}

---------- Post added at 11:17 PM ---------- Previous post was at 11:16 PM ----------

The output from this program is shown here:
ch is A
ch is B
ch is C
ch is D
ch is E
Notice that this example does not include the default case. Remember, the default is optional.
When not needed, it can be left out.
In C#, it is an error for the statement sequence associated with one case to continue on into
the next case. This is called the no fall-through rule. This is why case sequences end with break.

zied toumi
10-20-2013, 02:23
(You can avoid fall-through in other ways, but break is by far the most commonly used approach.)
When encountered within the statement sequence of a case, break causes program flow to exit
from the entire switch statement and resume at the next statement outside the switch. One other
point: The default sequence must also not “fall through,” and it, too, usually ends with a break.
Although you cannot allow one case sequence to fall through into another, you can have
two or more case labels for the same code sequence, as shown in this example:
switch(i) {
case 1:
case 2:
case 3: Console.WriteLine("i is 1, 2 or 3");
break;
case 4: Console.WriteLine("i is 4");
break;
}
In this fragment, if i has the value 1, 2, or 3, the first WriteLine( ) statement executes. If it is
4, the second WriteLine( ) statement executes. The stacking of cases does not violate the no
fall-through rule because the case statements all use the same statement sequence.
Stacking case labels is a commonly employed technique when several cases share common
code. For example, here it is used to categorize lowercase letters of the alphabet into vowels
and consonants:
// Categorize lowercase letters into vowels and consonants.

---------- Post added at 11:18 PM ---------- Previous post was at 11:17 PM ----------

using System;
class VowelsAndConsonants {
static void Main() {
char ch;
Console.Write("Enter a letter: ");
ch = (char) Console.Read();
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
Console.WriteLine("Letter is a vowel.");
break;
default:
Console.WriteLine("Letter is a consonant.");
break;
}
}
}

---------- Post added at 11:19 PM ---------- Previous post was at 11:18 PM ----------

Q: Under what conditions should I use an if-else-if ladder rather than a switch when
coding a multiway branch?
A: In general, use an if-else-if ladder when the conditions controlling the selection process do
not rely upon a single value. For example, consider the following if-else-if sequence:
if(x == 10) // ...
else if(ch == 'a') // ...
else if(done == true) // ...
This sequence cannot be recoded into a switch because all three conditions involve
different variables—and differing types. What variable would control the switch? Also,
you will need to use an if-else-if ladder when testing floating-point values or when testing
other objects that are not of types valid for use in a switch expression. Finally, the switch
can only test for equality. If you will be testing for some other relationship, such as less
than or not equal, you must use an if-else-if ladder. For example,
if(x < 10) // ...
else if(y >= 0) // ...
else if(z != -1) // ...
This sequence cannot be represented in a switch.

---------- Post added at 11:19 PM ---------- Previous post was at 11:19 PM ----------

If this example were written without case stacking, the same WriteLine( ) statement would
have been duplicated six times. The stacking of cases prevents this redundant duplication.
Nested switch Statements
It is possible to have a switch as part of the statement sequence of an outer switch. This is
called a nested switch. The case constants of the inner and outer switch can contain common
values, and no conflicts will arise. For example, the following code fragment is perfectly
acceptable:
switch(ch1) {
case 'A': Console.WriteLine("This A is part of outer switch.");
switch(ch2) {
case 'A':
Console.WriteLine("This A is part of inner switch");
break;
case 'B': // ...
} // end of inner switch
break;
case 'B': // ...

---------- Post added at 11:20 PM ---------- Previous post was at 11:19 PM ----------

Q: In C, C++, and Java, one case may continue on (that is, fall through) into the next case.
Why is this not allowed by C#?
A: There are two reasons that C# instituted the no fall-through rule for cases. First, it allows
the order of the cases to be rearranged. Such a rearrangement would not be possible if one
case could flow into the next. Second, requiring each case to explicitly end prevents
a programmer from accidentally allowing one case to flow into the next.

---------- Post added at 11:20 PM ---------- Previous post was at 11:20 PM ----------

Start Building a C# Help System
Here you will start building a simple help system that displays the syntax for the C# control
statements. This help system will be enhanced throughout the course of this chapter. This first
version displays a menu containing the control statements and then waits for you to choose
one. After one is chosen, the syntax of the statement is shown. In this first version of the
program, help is available only for the if and switch statements. The other control statements
are added later in subsequent Try This sections.
Step by Step
1. Create a file called Help.cs.
2. The program begins by displaying the following menu:
Help on:
1. if
2. switch
Choose one:
To accomplish this, you will use the statement sequence shown here:
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.Write("Choose one: ");
3. The program obtains the user’s selection by calling Console.Read( ), as shown here:
choice = (char) Console.Read();
4. Once the selection has been obtained, the program uses the switch statement shown here to
display the syntax for the selected statement:
switch(choice) {
case '1':
Console.WriteLine("The if:\n");

---------- Post added at 11:21 PM ---------- Previous post was at 11:20 PM ----------

Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
default:
Console.Write("Selection not found.");
break;
}
Notice how the default clause catches invalid choices. For example, if the user enters 3,
no case constants will match, causing the default sequence to execute.

---------- Post added at 11:21 PM ---------- Previous post was at 11:21 PM ----------

5. Here is the entire Help.cs program listing:
// A simple help system.
using System;
class Help {
static void Main() {
char choice;
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.Write("Choose one: ");
choice = (char) Console.Read();
Console.WriteLine("\n");
switch(choice) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");

---------- Post added at 11:22 PM ---------- Previous post was at 11:21 PM ----------

Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
default:
Console.Write("Selection not found.");
break;
}
}
}
Here is a sample run:
Help on:
1. if
2. switch
Choose one: 1
The if:
if(condition) statement;
else statement;

---------- Post added at 11:22 PM ---------- Previous post was at 11:22 PM ----------

The for Loop
You have been using a simple form of the for loop since Chapter 1. You might be surprised
at just how powerful and flexible the for loop is. Let’s begin by reviewing the basics, starting
with the most traditional forms of the for.
The general form of the for loop for repeating a single statement is
for(initialization; condition; iteration) statement;
For repeating a block, the general form is
for(initialization; condition; iteration)
{
statement sequence
}
The initialization is usually an assignment statement that sets the initial value of the loop
control variable, which acts as the counter that controls the loop. The condition is a Boolean
expression that determines whether the loop will repeat. The iteration expression defines the
amount by which the loop control variable will change each time the loop is repeated. Notice
that these three major sections of the loop must be separated by semicolons. The for loop will
continue to execute as long as the condition tests true. Once the condition becomes false, the
loop will exit, and program execution will resume on the statement following the for.

---------- Post added at 11:23 PM ---------- Previous post was at 11:22 PM ----------

The following program uses a for loop to print the square roots of the numbers between
1 and 99. It also displays the rounding error present for each square root.
// Show square roots of 1 to 99 and the rounding error.
using System;
class SqrRoot {
static void Main() {
double num, sroot, rerr;
for(num = 1.0; num < 100.0; num++) {
sroot = Math.Sqrt(num);
Console.WriteLine("Square root of " + num +
" is " + sroot);
// Compute rounding error.
rerr = num - (sroot * sroot);
Console.WriteLine("Rounding error is " + rerr);
Console.WriteLine();
}
}
}

zied toumi
10-20-2013, 02:27
Notice that the rounding error is computed by squaring the square root of each number.
This result is then subtracted from the original number, thus yielding the rounding error. Of
course, in some cases, rounding errors occur when the square root is squared, so sometimes
the rounding error, itself, is rounded! This example illustrates the fact that floating-point
calculations are not always as precise as we sometimes think they should be!
The for loop can proceed in a positive or negative fashion, and it can change the loop
control variable by any amount. For example, the following loop prints the numbers 100 to
–100, in decrements of 5:
// A negatively running for loop.
for(x = 100; x > -100; x -= 5)
Console.WriteLine(x);
}
An important point about for loops is that the conditional expression is always tested at
the top of the loop. This means that the code inside the loop may not be executed at all if the
condition is false to begin with. Here is an example:
for(count=10; count < 5; count++)
x += count; // this statement will not execute
This loop will never execute because its control variable, count, is greater than five when the
loop is first entered. This makes the conditional expression, count<5, false from the outset;
thus, not even one iteration of the loop will occur.

---------- Post added at 11:24 PM ---------- Previous post was at 11:23 PM ----------

Some Variations on the for Loop
The for is one of the most versatile statements in the C# language because it allows a wide
range of variations. For example, multiple loop control variables can be used. Consider the
following program:
// Use commas in a for statement.
using System;
class Comma {
static void Main() {
int i, j;
for(i=0, j=10; i < j; i++, j--)
Console.WriteLine("i and j: " + i + " " + j);
}
}
The output from the program is shown here:
i and j: 0 10
i and j: 1 9
i and j: 2 8
i and j: 3 7
i and j: 4 6
Here, commas separate the two initialization statements and the two iteration expressions.
When the loop begins, both i and j are initialized. Each time the loop repeats, i is incremented
and j is decremented. Multiple loop control variables are often convenient and can simplify
certain algorithms. You can have any number of initialization and iteration statements, but in
practice, more than two make the for loop unwieldy.
The condition controlling the loop can be any valid expression that produces a bool result.
It does not need to involve the loop control variable. In the next example, the loop continues to
execute until the user types S at the keyboard.
// Loop until an S is typed.

---------- Post added at 11:25 PM ---------- Previous post was at 11:24 PM ----------

using System;
class ForTest {
static void Main() {
int i;
Console.WriteLine("Press S to stop.");
for(i = 0; (char) Console.Read() != 'S'; i++)
Console.WriteLine("Pass #" + i);
}
}

---------- Post added at 11:25 PM ---------- Previous post was at 11:25 PM ----------

using System;
class ForTest {
static void Main() {
int i;
Console.WriteLine("Press S to stop.");
for(i = 0; (char) Console.Read() != 'S'; i++)
Console.WriteLine("Pass #" + i);
}
}

---------- Post added at 11:26 PM ---------- Previous post was at 11:25 PM ----------

In the next example, the initialization portion is also moved out of the for.
// Move more out of the for loop.
using System;
class Empty2 {
static void Main() {
int i;
i = 0; // move initialization out of loop
for(; i < 10; ) {

---------- Post added at 11:26 PM ---------- Previous post was at 11:26 PM ----------

Console.WriteLine("Pass #" + i);
i++; // increment loop control var
}
}
}
In this version, i is initialized before the loop begins, rather than as part of the for. Normally,
you will want to initialize the loop control variable inside the for. Placing the initialization
outside of the loop is generally done only when the initial value is derived through a complex
process that does not lend itself to containment inside the for statement.
The Infinite Loop
You can create an infinite loop (a loop that never terminates) using the for by leaving the
conditional expression empty. For example, the following fragment shows the way many C#
programmers create an infinite loop:
for(;;) // intentionally infinite loop
{
//...
}
This loop will run forever. Although there are some programming tasks that require an infinite
loop, such as operating-system command processors, most “infinite loops” are really just
loops with special termination requirements. Near the end of this chapter you will see how to
halt a loop of this type. (Hint: It’s done using the break statement.)
Loops with No Body
In C#, the body associated with a for loop (or any other loop) can be empty. This is because

---------- Post added at 11:27 PM ---------- Previous post was at 11:26 PM ----------

an empty statement is syntactically valid. Body-less loops are often useful. For example, the
following program uses one to sum the numbers 1 through 5:
// The body of a loop can be empty.
using System;
class Empty3 {
static void Main() {
int i;
int sum = 0;
// Sum the numbers through 5.
for(i = 1; i <= 5; sum += i++) ;
Console.WriteLine("Sum is " + sum);
}
}

AshrafQassim
10-20-2013, 04:30
السلام عليكم اخوتى ف الله
انا باعتقد ان المبتدالابد من قراءة كتب باللغة العربية
شكرا

zied tarhouni
10-20-2013, 06:41
السلام عليكم انا عندي 10دولار لكن فحسابي في انستا فوركس في غرفة العميل الخاصة مكتوب ان حساب لم يتفعل نو فيريفي لازم سورة نسخة من جواز سفر ?

taqi
10-20-2013, 18:00
بسم‏ ‏الله‏ ‏الرحمن‏ ‏الرحيم‏ ‏عاشت‏ ‏ايدك‏ ‏اخي‏ ‏العزيز‏ ‏استمر‏ ‏في‏ ‏مواضيعك‏ ‏الرائعة‏ ‏و‏ ‏لا‏ ‏تحرمنا‏ ‏من‏ ‏ابداعك‏ ‏في‏ ‏امان‏ ‏الله

mkacha50055
10-20-2013, 18:03
السلام عليكم و مشكور على الموضوع
لكن نحتاج في المنتدى الى مواضيع جديدة ليستفيد منها الجميع
و ليست مواضيع ماخوذة من منتديات اخرى
بالتوفيق للجميع . و السلام عليكم و رحمة الله

zied toumi
10-20-2013, 21:50
The output from the program is shown here:
Sum is 15
Notice that the summation process is handled entirely within the for statement and no body is
needed. *** special attention to the iteration expression:
sum += i++
Don’t be intimidated by statements like this. They are common in professionally written C#
programs and are easy to understand if you break them down into their parts. In words, this
statement says “add to sum the value of sum plus i, then increment i.” Thus, it is the same as
this sequence of statements:
sum = sum + i;
i++;
Declaring Loop Control Variables Inside the for Loop
Often, the variable that controls a for loop is needed only for the purposes of the loop and
is not used elsewhere. When this is the case, it is possible to declare the variable inside the
initialization portion of the for. For example, the following program computes both the
summation and the factorial of the numbers 1 through 5. It declares its loop control variable i
inside the for:

---------- Post added at 06:48 PM ---------- Previous post was at 06:47 PM ----------

// Declare loop control variable inside the for.
using System;
class ForVar {
static void Main() {
int sum = 0;
int fact = 1;
// Compute the factorial of the numbers through 5.
for(int i = 1; i <= 5; i++) {
sum += i; // i is known throughout the loop
fact *= i;
}
// Here, i is not known.
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Factorial is " + fact);
}
}
When you declare a variable inside a for loop, there is one important point to remember:
The scope of that variable ends when the for statement does. (That is, the scope of the variable
is limited to the for loop.) Outside the for loop, the variable will cease to exist. Thus, in the

---------- Post added at 06:49 PM ---------- Previous post was at 06:48 PM ----------

preceding example, i is not accessible outside the for loop. If you need to use the loop control
variable elsewhere in your program, you will not be able to declare it inside the for loop.
Before moving on, you might want to experiment with your own variations on the for
loop. As you will find, it is a fascinating loop.
The while Loop
Another of C#’s loops is the while. The general form of the while loop is
while(condition) statement;
where statement can be a single statement or a block of statements, and condition defines the
condition that controls the loop and may be any valid Boolean expression. The statement is
performed while the condition is true. When the condition becomes false, program control
passes to the line immediately following the loop.
Here is a simple example in which a while is used to print the alphabet:
// Demonstrate the while loop.

---------- Post added at 06:49 PM ---------- Previous post was at 06:49 PM ----------

using System;
class WhileDemo {
static void Main() {
char ch;
// Print the alphabet using a while loop.
ch = 'a';
while(ch <= 'z') {
Console.Write(ch);
ch++;
}
}
}
Here, ch is initialized to the letter a. Each time through the loop, ch is output and then
incremented. This process continues until ch is greater than z.
As with the for loop, the while checks the conditional expression at the top of the loop,
which means that the loop code may not execute at all. This often eliminates the need for
performing a separate test before the loop. The following program illustrates this characteristic
of the while loop. It computes the integer powers of 2 from 0 to 9.
// Compute integer powers of 2.
using System;
class Power {
static void Main() {

---------- Post added at 06:50 PM ---------- Previous post was at 06:49 PM ----------

int e;
int result;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
Console.WriteLine("2 to the " + i +
" power is " + result);
}
}
}
The output from the program is shown here:
2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512
Notice that the while loop executes only when e is greater than 0. Thus, when e is zero, as it is
in the first iteration of the for loop, the while loop is skipped.

---------- Post added at 06:50 PM ---------- Previous post was at 06:50 PM ----------

Q: Given the flexibility inherent in all of C#’s loops, what criteria should I use when
selecting a loop? That is, how do I choose the right loop for a specific job?
A: Use a for loop when performing a known number of iterations. Use the do-while when you
need a loop that will always perform at least one iteration. The while is best used when the
loop will repeat an unknown number of times.

ashrafshawky
10-20-2013, 21:57
اخى الكريم لك كل الشكر على هذا الموضوع الذى من الواضح انك قمت بجهد كبير فية بالفعل ولكن للاسف
انا خبرتى بالبرمجة قليلة جدل و بالتوفيق

zied toumi
10-20-2013, 22:13
The do-while Loop
The next loop is the do-while. Unlike the for and the while loops, in which the condition is
tested at the top of the loop, the do-while loop checks its condition at the bottom of the loop.
This means that a do-while loop will always execute at least once. The general form of the dowhile
loop is
do {
statements;
} while(condition);
Although the braces are not necessary when only one statement is present, they are often used
to improve readability of the do-while construct, thus preventing confusion with the while.
The do-while loop executes as long as the conditional expression is true.
The following program loops until the user enters the letter q:
// Demonstrate the do-while loop.
using System;
class DWDemo {
static void Main() {
char ch;
do {
Console.Write("Press a key followed by ENTER: ");
ch = (char) Console.Read(); // read a keypress
} while(ch != 'q');
}
}

---------- Post added at 07:01 PM ---------- Previous post was at 06:59 PM ----------

Using a do-while loop, we can further improve the guessing game program from earlier in
this chapter. This time, the program loops until you guess the letter.
// Guess the letter game, 4th version.
using System;
class Guess4 {
static void Main() {
char ch, answer = 'K';
do {
Console.WriteLine("I'm thinking of a letter between A and Z.");
Console.Write("Can you guess it: ");
// Read a letter, but skip cr/lf.

---------- Post added at 07:08 PM ---------- Previous post was at 07:01 PM ----------

do {
ch = (char) Console.Read();
} while(ch == '\n' | ch == '\r');
if(ch == answer) Console.WriteLine("** Right **");
else {
Console.Write("...Sorry, you're ");
if(ch < answer) Console.WriteLine("too low");
else Console.WriteLine("too high");
Console.WriteLine("Try again!\n");
}
} while(answer != ch);
}
}
Here is a sample run:
I'm thinking of a letter between A and Z.
Can you guess it: A
...Sorry, you're too low
Try again!
I'm thinking of a letter between A and Z.
Can you guess it: Z
...Sorry, you're too high
Try again!
I'm thinking of a letter between A and Z.
Can you guess it: K
** Right **

---------- Post added at 07:09 PM ---------- Previous post was at 07:08 PM ----------

Notice one other thing of interest in this program. The do-while loop shown here obtains
the next character, skipping over any carriage-return and linefeed characters that might be in
the input stream:
// read a letter, but skip cr/lf
do {
ch = (char) Console.Read(); // get a char
} while(ch == '\n' | ch == '\r');
Here is why this loop is needed. As explained earlier, console input is line-buffered—you have
to press ENTER before characters are sent. Pressing ENTER causes a carriage-return and a linefeed
character to be generated. These characters are left pending in the input buffer. This loop
discards those characters by continuing to read input until neither is present.

---------- Post added at 07:09 PM ---------- Previous post was at 07:09 PM ----------

Improve the C# Help System
This program expands on the C# help system that was begun in the previous Try This section.
This version adds the syntax for the for, while, and do-while loops. It also checks the user’s
menu selection, looping until a valid response is entered.
Step by Step
1. Copy Help.cs to a new file called Help2.cs.
2. Change the portion of the program that displays the choices so that it uses the loop
shown here:
do {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while\n");
Console.Write("Choose one: ");
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '5');
Notice that a nested do-while loop is used to discard any spurious carriage-return or linefeed
characters that may be present in the input stream. After making this change, the program
will loop, displaying the menu until the user enters a response that is between 1 and 5.

---------- Post added at 07:10 PM ---------- Previous post was at 07:09 PM ----------

3. Expand the switch statement to include the for, while, and do-while loops, as shown here:
switch(choice) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");

---------- Post added at 07:10 PM ---------- Previous post was at 07:10 PM ----------

Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;
case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
}
Notice that no default is present in this version of the switch. Since the menu loop ensures
that a valid response will be entered, it is no longer necessary to include a default sequence
to handle an invalid choice.

---------- Post added at 07:11 PM ---------- Previous post was at 07:10 PM ----------

4. Here is the entire Help2.cs program listing:
/*
An improved Help system that uses a
do-while to process a menu selection.
*/
using System;
class Help2 {
static void Main() {
char choice;
do {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while\n");
Console.Write("Choose one: ");
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '5');
Console.WriteLine("\n");
switch(choice) {
case '1':
Console.WriteLine("The if:\n"); (continued)

---------- Post added at 07:11 PM ---------- Previous post was at 07:11 PM ----------

Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;
case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
}
}
}

---------- Post added at 07:12 PM ---------- Previous post was at 07:11 PM ----------

Use break to Exit a Loop
It is possible to force an immediate exit from a loop, bypassing any code remaining in the
body of the loop and the loop’s conditional test, by using the break statement. When a break
statement is encountered inside a loop, the loop is terminated and program control resumes at
the next statement following the loop. Here is a simple example:
// Using break to exit a loop.
using System;
class BreakDemo {
static void Main() {
int num;

---------- Post added at 07:12 PM ---------- Previous post was at 07:12 PM ----------

num = 100;
// Loop while i squared is less than num.
for(int i=0; i < num; i++) {
// Terminate loop if i*i >= 100.
if(i*i >= num) break;
Console.Write(i + " ");
}
Console.WriteLine("Loop complete.");
}
}
This program generates the following output:
0 1 2 3 4 5 6 7 8 9 Loop complete.

---------- Post added at 07:13 PM ---------- Previous post was at 07:12 PM ----------

As you can see, although the for loop is designed to run from 0 to num (which, in this case, is
100), the break statement causes it to terminate early, when i squared is greater than or equal
to num.
The break statement can be used with any of C#’s loops, including intentionally infinite
loops. For example, the following program simply reads input until the user presses q:
// Read input until a q is received.
using System;
class Break2 {
static void Main() {
char ch;
for( ; ; ) {
ch = (char) Console.Read();
if(ch == 'q') break;
}
Console.WriteLine("You pressed q!");
}
}
When used inside a set of nested loops, the break statement will break out of only the
innermost loop. For example:
// Using break with nested loops.
using System;
class Break3 {
static void Main() {

---------- Post added at 07:13 PM ---------- Previous post was at 07:13 PM ----------

for(int i=0; i<3; i++) {
Console.WriteLine("Outer loop count: " + i);
Console.Write(" Inner loop count: ");
int t = 0;
while(t < 100) {
if(t == 10) break;
Console.Write(t + " ");
t++;
}
Console.WriteLine();
}
Console.WriteLine("Loops complete.");
}
}
This program generates the following output:
Outer loop count: 0
Inner loop count: 0 1 2 3 4 5 6 7 8 9

zied toumi
10-20-2013, 22:16
Outer loop count: 1
Inner loop count: 0 1 2 3 4 5 6 7 8 9
Outer loop count: 2
Inner loop count: 0 1 2 3 4 5 6 7 8 9
Loops complete.
As you can see, the break statement in the inner loop causes only the termination of that loop.
The outer loop is unaffected.
Here are two other points to remember about break. First, more than one break statement
may appear in a loop, but be careful. Too many break statements have the tendency to
destructure your code. Second, the break that exits a switch statement affects only that switch
statement and not any enclosing loops.
Q: I know that in Java, the break and continue statements can be used with a label. Does
C# support the same feature?
A: No. The designers of C# did not give break or continue that capability. Instead, break and
continue work the same in C# as they do in C and C++. One reason that C# did not follow
Java’s lead on this issue is that Java does not support the goto statement, but C# does. Thus,
Java needs to give break and continue extra power to make up for the lack of the goto.

---------- Post added at 07:15 PM ---------- Previous post was at 07:14 PM ----------

It is possible to force an early iteration of a loop, bypassing the loop’s normal control structure.
This is accomplished using continue. The continue statement forces the next iteration
of the loop to take place, skipping any code in between. Thus, continue is essentially the
complement of break. For example, the following program uses continue to help print the
even numbers between 0 and 100:
// Use continue.
using System;
class ContDemo {
static void Main() {
int i;
// Print even numbers between 0 and 100.
for(i = 0; i<=100; i++) {
// Iterate if i is odd.
if((i%2) != 0) continue;
Console.WriteLine(i);
}
}
}

---------- Post added at 07:15 PM ---------- Previous post was at 07:15 PM ----------

Only even numbers are printed, because an odd one will cause the loop to iterate early, bypassing
the call to WriteLine( ).
In while and do-while loops, a continue statement will cause control to go directly to the
conditional expression. In the case of the for, the iteration expression of the loop is evaluated
and then the conditional expression is executed.
Good uses of continue are rare. One reason is that C# provides a rich set of loop statements
that fit most applications. However, for those special circumstances in which early iteration is
needed, the continue statement provides a structured way to accomplish it.
The goto
The goto is C#’s unconditional jump statement. When encountered, program flow jumps to the
location specified by the goto. The statement fell out of favor with programmers many years ago
because it encouraged the creation of “spaghetti code,” a tangled mess of unconditional jumps
that resulted in hard-to-follow code. However, the goto is still occasionally—and sometimes
effectively—used. This book will not make a judgment regarding its validity as a form of program
control. It should be stated, however, that there are no programming situations that require the use
of the goto statement—it is not an item necessary for making the language complete. Rather, it is
a convenience, which, if used wisely, can be of benefit in certain programming situations. As such,
the goto is not used in this book outside of this section. The chief concern most programmers have

---------- Post added at 07:16 PM ---------- Previous post was at 07:15 PM ----------

about the goto is its tendency to clutter a program and render it nearly unreadable. However, there
are times when the use of the goto can clarify program flow rather than confuse it.
The goto requires a label for operation. A label is a valid C# identifier followed by a colon.
Furthermore, the label must be in the same method as the goto that uses it. For example, a loop
from 1 to 100 could be written using a goto and a label, as shown here:
x = 1;
loop1:
x++;
if(x < 100) goto loop1;
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
imagine real-world situations in which a goto might be beneficial.
The goto does have one important restriction: You cannot jump into a block. Of course,
you can jump out of a block, as the preceding example shows.

---------- Post added at 07:16 PM ---------- Previous post was at 07:16 PM ----------

In addition to working with “normal” labels, the goto can be used to jump to a case or
default label within a switch. For example, this is a valid switch statement:
switch(x) {
case 1: // ...
goto default;
case 2: // ...
goto case 1;
default: // ...
break;
}
The goto default jumps to the default label. The goto case 1 jumps to case 1. Because of the
restriction stated above, you cannot jump into the middle of a switch from code outside the
switch because a switch defines a block. Therefore, these types of goto statements must be
executed from within the switch.

anwarforex5
10-20-2013, 22:17
ان الكتب في اللغة سي هي ستكون جيدة بخصوص هذه البرمجة
لكن اعتقد ان البمرجة لازم لها من التطبيق لكي يفهم جيدا

zied toumi
10-20-2013, 22:37
Finish the C# Help System
Here you will put the finishing touches on the C# help system. This version adds the syntax for
break, continue, and goto. It also allows the user to request the syntax for more than one statement.
It does this by adding an outer loop that runs until the user enters a q as a menu selection.
Step by Step
1. Copy Help2.cs to a new file called Help3.cs.
2. Surround all of the program code with an infinite for loop. Break out of this loop, using
break, when a q is entered. Since this loop surrounds all of the program code, breaking out
of this loop causes the program to terminate.
3. Change the menu loop, as shown here:
do {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while");
Console.WriteLine(" 6. break");
Console.WriteLine(" 7. continue");
Console.WriteLine(" 8. goto\n");
Console.Write("Choose one (q to quit): ");
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '8' & choice != 'q');
(

---------- Post added at 07:19 PM ---------- Previous post was at 07:17 PM ----------

Notice that this loop now includes the break, continue, and goto statements. It also accepts
a q as a valid choice.
4. Expand the switch statement to include the break, continue, and goto statements, as
shown here:
case '6':
Console.WriteLine("The break:\n");
Console.WriteLine("break;");
break;
case '7':
Console.WriteLine("The continue:\n");
Console.WriteLine("continue;");
break;
case '8':
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
5. Here is the entire Help3.cs program listing:
/*
The finished C# statement help system
that processes multiple requests.
*/

---------- Post added at 07:20 PM ---------- Previous post was at 07:19 PM ----------

using System;
class Help3 {
static void Main() {
char choice;
for(;;) {
do {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while");
Console.WriteLine(" 6. break");
Console.WriteLine(" 7. continue");
Console.WriteLine(" 8. goto\n");
Console.Write("Choose one (q to quit): ");
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '8' & choice != 'q');
if(choice == 'q') break;

---------- Post added at 07:24 PM ---------- Previous post was at 07:20 PM ----------

إني أحاول أن أشرح و أعطي أمثلة لما أنا بصدد شرحه و إنشاء الله أكون قد وفقت ولو بنسبة ظعيفة

---------- Post added at 07:25 PM ---------- Previous post was at 07:24 PM ----------

Console.WriteLine("\n");
switch(choice) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;
case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case '6':
Console.WriteLine("The break:\n");
Console.WriteLine("break;");
break;
case '7':
Console.WriteLine("The continue:\n");
Console.WriteLine("continue;");
break;
case '8':
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}
}
}

---------- Post added at 07:26 PM ---------- Previous post was at 07:25 PM ----------

Here is a sample run:
Help on:
1. if
2. switch
3. for
4. while
5. do-while
6. break
7. continue
8. goto
Choose one (q to quit): 1
The if:
if(condition) statement;
else statement;
Help on:
1. if
2. switch
3. for
4. while
5. do-while
6. break
7. continue
8. goto

---------- Post added at 07:32 PM ---------- Previous post was at 07:26 PM ----------

Choose one (q to quit): 6
The break:
break;
Help on:
1. if
2. switch
3. for
4. while
5. do-while
6. break
7. continue
8. goto
Choose one (q to quit): q

---------- Post added at 07:32 PM ---------- Previous post was at 07:32 PM ----------

As you have seen in some of the preceding examples, one loop can be nested inside another.
Nested loops are used to solve a wide variety of programming problems and are an essential
part of programming. So, before leaving the topic of C#’s loop statements, let’s look at one
more nested loop example. The following program uses a nested for loop to find the factors of
the numbers from 2 to 100:
// Use nested loops to find factors of numbers between 2 and 100.
using System;
class FindFac {
static void Main() {
for(int i=2; i <= 100; i++) {
Console.Write("Factors of " + i + ": ");
for(int j = 2; j <= i/2; j++)
if((i%j) == 0) Console.Write(j + " ");
Console.WriteLine();
}
}
}
Here is a portion of the output produced by the program:
Factors of 2:
Factors of 3:
Factors of 4: 2
Factors of 5:
Factors of 6: 2 3
Factors of 7:
Factors of 8: 2 4
Factors of 9: 3
Factors of 10: 2 5
Factors of 11:
Factors of 12: 2 3 4 6
Factors of 13:
Factors of 14: 2 7
Factors of 15: 3 5
Factors of 16: 2 4 8
Factors of 17:
Factors of 18: 2 3 6 9
Factors of 19:
Factors of 20: 2 4 5 10
In the program, the outer loop runs i from 2 through 100. The inner loop successively tests all
numbers from 2 up to i, printing those that evenly divide i.

---------- Post added at 07:35 PM ---------- Previous post was at 07:32 PM ----------

Chapter 3 Self Test
1. Write a program that reads characters from the keyboard until a period is received. Have the
program count the number of spaces. Report the total at the end of the program.
2. In the switch, can the code sequence from one case run into the next?
3. Show the general form of the if-else-if ladder.
4. Given
if(x < 10)
if(y > 100) {
if(!done) x = z;
else y = z;
}
else Console.WriteLine("error"); // what if?
with what if does the last else associate?
5. Show the for statement for a loop that counts from 1,000 to 0 by –2.
6. Is the following fragment valid?
for(int i = 0; i < num; i++)
sum += i;
count = i;

---------- Post added at 07:35 PM ---------- Previous post was at 07:35 PM ----------

10. The iteration expression in a for loop need not always alter the loop control variable by a
fixed amount. Instead, the loop control variable can change in any arbitrary way. Using this
concept, write a program that uses a for loop to generate and display the progression 1, 2, 4,
8, 16, 32, and so on.
11. The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to convert
a lowercase letter to uppercase, subtract 32 from it. Use this information to write a program
that reads characters from the keyboard. Have it convert all lowercase letters to uppercase,
and all uppercase letters to lowercase, displaying the result. Make no changes to any other
character. Have the program stop when the user presses the period key. At the end, have the
program display the number of case changes that have taken place.

---------- Post added at 07:36 PM ---------- Previous post was at 07:35 PM ----------

Chapter 4
Introducing Classes,
Objects, and Methods

---------- Post added at 07:37 PM ---------- Previous post was at 07:36 PM ----------

Key Skills & Concepts
● Class fundamentals
● Instantiate an object
● Method basics
● Method parameters
● Return a value from a method
● Constructors
● new
● Garbage collection
● Destructors
● The this keyword
Before you can go much further in your study of C#, you need to learn about the class. The
class is the essence of C# because it defines the nature of an object. As such, the class
forms the basis for object-oriented programming in C#. Within a class are defined data and
the code that acts upon that data. The code is contained in methods. Because classes, objects,
and methods are fundamental to C#, they are introduced in this chapter. Having a basic
understanding of these features will allow you to write more sophisticated programs and to
better understand certain key C# elements described in Chapter 5.

zied toumi
10-20-2013, 22:38
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. As
you will see, 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 typically specifies both code and data, with the code acting on the 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.
One other point: Recall that the methods and variables that constitute a class are called
members of the class.

---------- Post added at 07:38 PM ---------- Previous post was at 07:37 PM ----------

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 instance variables defined by the class, and code is
contained in methods. It is important to state at the outset, however, that C# defines several
specific flavors of members, which include instance variables, static variables, constants,
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.
A class is created by using the keyword class. The general form of a class definition that
contains only instance variables and methods is shown here:
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 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 1, 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,
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. This means they can be used by all other code—even code
defined outside the class. (The Main( ) method will continue to use the default access because
this is the currently recommended approach.) We will return to the topic of access specifiers in
a later chapter, after you have learned the fundamentals of the class.
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( ).
Soon you will see how to create others. 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 evolve a class that encapsulates information about vehicles,
such as cars, vans, and trucks. This class is called Vehicle, and it will store three items of
information about a vehicle: the number of passengers that it can carry, its fuel capacity, and its
average fuel consumption (in miles per gallon).
The first version of Vehicle is shown here. It defines three instance variables: Passengers,
FuelCap, and Mpg. Notice that Vehicle does not contain any methods. Thus, it is currently
a data-only class. (Subsequent sections will add methods to it.)

---------- Post added at 07:38 PM ---------- Previous post was at 07:38 PM ----------

class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
}
The instance variables defined by Vehicle illustrate the way that instance variables are
declared in general. The general form for declaring an instance variable is shown here:
access type var-name;
Here, access specifies the access, type specifies the type of variable, and var-name is the
variable’s name. Thus, aside from the access specifier, you declare an instance variable in the
same way that you declare local variables. For Vehicle, the variables are preceded by the public
access modifier. As explained, this allows them to be accessed by code outside of Vehicle.
A class definition creates a new data type. In this case, the new data type is called Vehicle.
You will use this name to declare objects of type Vehicle. Remember that a class declaration is
only a type description; it does not create an actual object. Thus, the preceding code does not
cause any objects of type Vehicle to come into existence.
To actually create a Vehicle object, you will use a statement like the following:
Vehicle minivan = new Vehicle(); // create a Vehicle object called minivan
After this statement executes, minivan will be an instance of Vehicle. Thus, it will have
“physical” reality. For the moment, don’t worry about the details of this statement.

olever
10-20-2013, 22:43
In this quest, C# is the current standard bearer. Created by Microsoft to support development
for its .NET Framework, C# leverages time-tested features with cutting-edge innovations and
provides a highly usable, efficient way to write programs for the modern enterprise computing
environment. In the course of this book, you will learn to program using it.
The purpose of this chapter is to introduce C#, including the forces that drove its creation,
its design philosophy, and several of its most important features. By far, the hardest thing
about learning a programming language is the fact that no element exists in isolation. Instead,
the components of the language work together. It is this interrelatedness that makes it difficult
to discuss one aspect of C# without involving others. To overcome this problem, this chapter
provides a brief overview of several C# features, including the general form of a C# program,
two control statements, and several operators. It does not go into too many details, but rather
concentrates on the general concepts common to any C# program.
At this time, version 3.0 is the current release of C#, and this is the version taught in this
book. Of course, much of the information presented here applies to all versions of C#.
C#’s Family Tree
Computer languages do not exist in a void. Rather, they relate to one another, with each new
language influenced in one form or another by the ones that came before. In a process akin
to cross-pollination, features from one language are adapted by another, a new innovation is
integrated into an existing context, or an older construct is removed. In this way, languages
evolve and the art of programming advances. C# is no exception.

---------- Post added at 08:43 PM ---------- Previous post was at 08:39 PM ----------

السلام عايكم اخي الكريم انا مبتدئ في هدا المجال

zied toumi
10-20-2013, 23:40
Each time you create an instance of a class, you are creating an object that contains its own
copy of each instance variable defined by the class. Thus, every Vehicle object will contain
its own copies of the instance variables Passengers, FuelCap, and Mpg. To access these
variables, you will use the member access operator, which is a period. It is commonly referred
to as the dot operator. The dot operator links the name of an object with the name of a member.
The general form of the dot operator is shown here:
object.member
Thus, the object is specified on the left, and the member is put on the right. For example, to
assign the FuelCap variable of minivan the value 16, use the following statement:
minivan.FuelCap = 16;
In general, you can use the dot operator to access both instance variables and methods.
Here is a complete program that uses the Vehicle class:
/* A program that uses the Vehicle class.
Call this file UseVehicle.cs
*/

---------- Post added at 07:52 PM ---------- Previous post was at 07:50 PM ----------

using System;
// A class that encapsulates information about vehicles.
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
}
// This class declares an object of type Vehicle.
class VehicleDemo {
static void Main() {
Vehicle minivan = new Vehicle();
int range;
// Assign values to fields in minivan.
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
// Compute the range assuming a full tank of gas.
range = minivan.FuelCap * minivan.Mpg;
Console.WriteLine("Minivan can carry " + minivan.Passengers +
" with a range of " + range);
}
}

---------- Post added at 07:52 PM ---------- Previous post was at 07:52 PM ----------

This program consists of two classes: Vehicle and VehicleDemo. Inside VehicleDemo, the
Main( ) method creates an instance of Vehicle called minivan. Then the code within Main( )
accesses the instance variables associated with minivan, assigning them values and using
those values. It is important to understand that Vehicle and VehicleDemo are two separate
classes. The only relationship they have to each other is that one class creates an instance
of the other. Although they are separate classes, code inside VehicleDemo can access the
members of Vehicle because they are declared public. If they had not been given the public
access specifier, their access would have been limited to the Vehicle class, and VehicleDemo
would not have been able to use them.
Assuming that you call the preceding file UseVehicle.cs, compiling this program creates
a file called UseVehicle.exe. Both the Vehicle and VehicleDemo classes are automatically part
of the executable file. The program displays the following output:
Minivan can carry 7 with a range of 336
It is not necessary for both the Vehicle and the VehicleDemo classes to actually be in the
same source file. You could put each class in its own file, called Vehicle.cs and VehicleDemo.cs,
for example. Just tell the C# compiler to compile both files and link them together. For
example, you could use this command line to compile the program if you split it into two
pieces as just described:

---------- Post added at 08:02 PM ---------- Previous post was at 07:52 PM ----------

csc Vehicle.cs VehicleDemo.cs
This will create VehicleDemo.exe because that is the class that contains the Main( ) method.
If you are using the Visual Studio IDE, you will need to add both files to your project and
then build.
Before moving on, let’s review a fundamental principle: Each object has its own copies of
the instance variables defined by its class. Thus, the contents of the variables in one object can
differ from the contents of the variables in another. There is no connection between the two
objects, except for the fact that they are both objects of the same type. For example, if you have
two Vehicle objects, each has its own copy of Passengers, FuelCap, and Mpg, and the contents
of these can differ between the two objects. The following program demonstrates this fact:
// This program creates two Vehicle objects.
using System;
// A class that encapsulates information about vehicles.
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
}
// This class declares two objects of type Vehicle.
class TwoVehicles {

---------- Post added at 08:03 PM ---------- Previous post was at 08:02 PM ----------

static void Main() {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2;
// Assign values to fields in minivan.
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
// Assign values to fields in sportscar.
sportscar.Passengers = 2;
sportscar.FuelCap = 14;
sportscar.Mpg = 12;
// Compute the ranges assuming a full tank of gas.
range1 = minivan.FuelCap * minivan.Mpg;
range2 = sportscar.FuelCap * sportscar.Mpg;
Console.WriteLine("Minivan can carry " + minivan.Passengers +
" with a range of " + range1);
Console.WriteLine("Sportscar can carry " + sportscar.Passengers +
" with a range of " + range2);
}
}

---------- Post added at 08:08 PM ---------- Previous post was at 08:03 PM ----------

The output produced by this program is shown here:
Minivan can carry 7 with a range of 336
Sportscar can carry 2 with a range of 168
As you can see, minivan’s data is completely separate from the data contained in sportscar.

---------- Post added at 08:25 PM ---------- Previous post was at 08:08 PM ----------

How Objects Are Created
In the preceding programs, the following line was used to create an object of type Vehicle:
Vehicle minivan = new Vehicle();
This declaration performs three functions. First, it declares a variable called minivan of the
class type Vehicle. This variable is not, itself, an object. Instead, it is simply a variable that can
refer to an object. Second, the declaration creates an actual, physical instance of the object.
This is done by using the new operator. Finally, it assigns to minivan a reference to that
object. Thus, after the line executes, minivan refers to an object of type Vehicle.
The new operator dynamically allocates (that is, allocates at runtime) memory for an
object and returns a reference to it. This reference is then stored in a variable. Thus, in C#, all
class objects must be dynamically allocated.
As you might expect, it is possible to separate the declaration of minivan from the creation
of the object to which it will refer, as shown here:
Vehicle minivan; // declare a reference to an object
minivan = new Vehicle(); // allocate a Vehicle object
The first line declares minivan as a reference to an object of type Vehicle. Thus, minivan is
a variable that can refer to an object, but it is not an object itself. The next line creates a new
Vehicle object and assigns a reference to it to minivan. Now, minivan is linked with an object.
The fact that class objects are accessed through a reference explains why classes are called
reference types. The key difference between value types and reference types is what a variable
of each type means. For a variable of a value type, the variable itself contains the value. For
example, given
int x;
x = 10;

---------- Post added at 08:39 PM ---------- Previous post was at 08:25 PM ----------

x contains the value 10 because x is a variable of type int, which is a value type. However, in
the case of
Vehicle minivan = new Vehicle();
minivan does not, itself, contain the object. Instead, it contains a reference to the object.
Reference Variables and Assignment
In an assignment operation, reference variables act differently than do variables of a value
type, such as int. When you assign one value type variable to another, the situation is
straightforward. The variable on the left receives a copy of the value of the variable on the
right. When you assign one object reference variable to another, the situation is a bit more
complicated because you are causing the variable on the left to refer to the object referred

---------- Post added at 08:40 PM ---------- Previous post was at 08:39 PM ----------

to by the variable on the right. The effect of this difference can cause some counterintuitive
results. For example, consider the following fragment:
Vehicle car1 = new Vehicle();
Vehicle car2 = car1;
At first glance, it is easy to think that car1 and car2 refer to different objects, but this is not the
case. Instead, car1 and car2 will both refer to the same object. The assignment of car1 to car2
simply makes car2 refer to the same object as does car1. Thus, the object can be acted upon
by either car1 or car2. For example, after this assignment executes,
car1.Mpg = 26;
both of these WriteLine( ) statements display the same value, 26:
Console.WriteLine(car1.Mpg);
Console.WriteLine(car2.Mpg);
Although car1 and car2 both refer to the same object, they are not linked in any other way.
For example, a subsequent assignment to car2 simply changes what object car2 refers to. For
example:
Vehicle car1 = new Vehicle();
Vehicle car2 = car1;
Vehicle car3 = new Vehicle();

salim telecom
10-20-2013, 23:43
السلام عليكم أخي الكريم شكرا لك اخي الكريم بارك الله فيك الموضوع ممتاز لاكن ياليت لو كان مترجم إلي العربية و الله أعلم و الله الموفق

zied toumi
10-21-2013, 14:14
car2 = car3; // now car2 and car3 refer to the same object.
After this sequence executes, car2 refers to the same object as car3. Of course, car1 continues
to refer to its original object.
Methods
As explained, instance variables and methods are two of the primary constituents of classes. So
far, the Vehicle class contains data, but no methods. Although data-only classes are perfectly
valid, most classes will have methods. Methods are subroutines that manipulate the data
defined by the class and, in many cases, provide access to that data. Typically, other parts of
your program will interact with a class through its methods.
A method contains one or more statements. In well-written C# code, each method performs
only one task. Each method has a name, and this name is used to call the method. In general,
you can name a method using any valid identifier that you please. However, remember that
Main( ) is reserved for the method that begins execution of your program. Also, don’t use C#’s
keywords for method names.
When denoting methods in text, this book has used and will continue to use a convention
that has become common when writing about C#. A method will have parentheses after its
name. For example, if a method’s name is GetVal, it will be written GetVal( ) when its name

---------- Post added at 10:39 AM ---------- Previous post was at 10:36 AM ----------

is used in a sentence. This notation will help you distinguish variable names from method
names in this book.
The general form of a method is shown here:
access ret-type name(parameter-list) {
// body of method
}
Here, access is an access modifier that governs what other parts of your program can call the
method. As explained earlier, the access modifier is optional. If not present, the method is private
to the class in which it is declared. For now, we will declare methods public so that they can be
called by any other code in the program. The ret-type specifies the type of data returned by the
method. This can be any valid type, including class types that you create. If the method does
not return a value, its return type must be void. The name of the method is specified by name.
This can be any legal identifier other than those which would cause conflicts within the current
declaration space. The parameter-list is a sequence of type and identifier pairs separated by
commas. Parameters are essentially variables that receive the value of the arguments passed to
the method when it is called. If the method has no parameters, the parameter list will be empty.

---------- Post added at 10:41 AM ---------- Previous post was at 10:39 AM ----------

Add a Method to the Vehicle Class
As just explained, the methods of a class typically manipulate and provide access to the data
of the class. With this in mind, recall that Main( ) in the preceding examples computed the range
of a vehicle by multiplying its fuel consumption rate by its fuel capacity. While technically
correct, this is not the best way to handle this computation. The calculation of a vehicle’s range is
something that is best handled by the Vehicle class itself. The reason for this conclusion is easy
to understand: The range of a vehicle is dependent upon the capacity of the fuel tank and the
rate of fuel consumption, and both of these quantities are encapsulated by Vehicle. By adding a
method to Vehicle that computes the range, you are enhancing its object-oriented structure.
To add a method to Vehicle, specify it within Vehicle’s declaration. For example, the following
version of Vehicle contains a method called Range( ) that displays the range of the vehicle:
// Add range to Vehicle.
using System;
// A class that encapsulates information about vehicles.
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
// Display the range.
public void Range() {
Console.WriteLine("Range is " + FuelCap * Mpg);
}
}
Notice that FuelCap and Mpg are used
directly, without the dot operator.

---------- Post added at 10:45 AM ---------- Previous post was at 10:41 AM ----------

class AddMeth {
static void Main() {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
// Assign values to fields in minivan.
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
// Assign values to fields in sportscar.
sportscar.Passengers = 2;
sportscar.FuelCap = 14;
sportscar.Mpg = 12;
Console.Write("Minivan can carry " + minivan.Passengers +
". ");
minivan.Range(); // display range of minivan
Console.Write("Sportscar can carry " + sportscar.Passengers +
". ");
sportscar.Range(); // display range of sportscar.
}
}
This program generates the following output:
Minivan can carry 7. Range is 336
Sportscar can carry 2. Range is 168

---------- Post added at 10:49 AM ---------- Previous post was at 10:45 AM ----------

Let’s look at the key elements of this program, beginning with the Range( ) method itself.
The first line of Range( ) is
public void Range() {
This line declares a method called Range that has no parameters. It is specified as public, so it
can be used by all other parts of the program. Its return type is void. Thus, Range( ) does not
return a value to the caller. The line ends with the ****ing curly brace of the method body.
The body of Range( ) consists solely of this line:
Console.WriteLine("Range is " + FuelCap * Mpg);
This statement displays the range of the vehicle by multiplying FuelCap by Mpg. Since each
object of type Vehicle has its own copy of FuelCap and Mpg, when Range( ) is called, the
range computation uses the calling object’s copies of those variables.

---------- Post added at 11:12 AM ---------- Previous post was at 10:49 AM ----------

The Range( ) method ends when its closing curly brace is encountered. This causes
program control to transfer back to the caller.
Next, look closely at this line of code from inside Main( ):
minivan.Range();
This statement invokes the Range( ) method on minivan. That is, it calls Range( ) relative
to the object referred to by minivan, by use of the dot operator. When a method is called,
program control is transferred to the method. When the method terminates, control is
transferred back to the caller, and execution resumes with the line of code following the call.
In this case, the call to minivan.Range( ) displays the range of the vehicle defined by
minivan. In a similar fashion, the call to sportscar.Range( ) displays the range of the vehicle
defined by sportscar. Each time Range( ) is invoked, it displays the range for the specified object.
There is something very important to notice inside the Range( ) method: The instance
variables FuelCap and Mpg are referred to directly, without use of the dot operator. When
a method uses an instance variable that is defined by its class, it does so directly, without
explicit reference to an object and without use of the dot operator. This is easy to understand
if you think about it. A method is always invoked relative to some object of its class. Once
this invocation has occurred, the object is known. Thus, within a method, there is no need
to specify the object a second time. This means that FuelCap and Mpg inside Range( )
implicitly refer to the copies of those variables found in the object that invokes Range( ).

---------- Post added at 11:14 AM ---------- Previous post was at 11:12 AM ----------

Return from a Method
In general, there are two conditions that cause a method to return. The first, as the Range( )
method in the preceding example shows, occurs when the method’s closing curly brace is
encountered. The second is when a return statement is executed. There are two forms of
return: one for use in void methods (those that do not return a value) and one for returning
values. The first form is examined here. The next section explains how to return values.
In a void method, you can cause the immediate termination of a method by using this form
of return:
return ;
When this statement executes, program control returns to the caller, skipping any remaining
code in the method. For example, consider this method:
public void MyMeth() {
int i;
for(i=0; i<10; i++) {
if(i == 5) return; // stop at 5
Console.WriteLine(i);
}
}
Here, the for loop will only run from 0 to 5, because once i equals 5, the method returns.

---------- Post added at 11:14 AM ---------- Previous post was at 11:14 AM ----------

It is permissible to have multiple return statements in a method, especially when there are
two or more routes out of it. For example,
public void MyMeth() {
// ...
if(done) return;
// ...
if(error) return;
}
Here, the method returns if it is done or if an error occurs. Be careful, however, because having
too many exit points in a method can destructure your code, so avoid using them casually.
To review: A void method can return in one of two ways: its closing curly brace is reached,
or a return statement is executed.

zied toumi
10-21-2013, 14:50
Return a Value
Although methods with a return type of void are not rare, most methods will return a value.
In fact, the ability to return a value is one of the most useful features of a method. You have
already seen an example of a return value: when we used Math.Sqrt( ) to obtain a square root
in chapters 2 and 3.
Return values are used for a variety of purposes in programming. In some cases, such
as with Math.Sqrt( ), the return value contains the outcome of some calculation. In other
cases, the return value may simply indicate success or failure. In still others, it may contain
a status code. Whatever the purpose, using method return values is an integral part of C#
programming.
Methods return a value to the calling routine using this form of return:
return value;
Here, value is the value returned.
You can use a return value to improve the implementation of Range( ). Instead of
displaying the range, a better approach is to have Range( ) compute the range and return
this value. Among the advantages to this approach is that you can use the value for other
calculations. The following example modifies Range( ) to return the range rather than
displaying it:
// Use a return value.
using System;
// A class that encapsulates information about vehicles.
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon

---------- Post added at 11:17 AM ---------- Previous post was at 11:17 AM ----------

// Return the range.
public int Range() {
return Mpg * FuelCap;
}
}
class RetMeth {
static void Main() {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2;
// Assign values to fields in minivan.
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
// Assign values to fields in sportscar.
sportscar.Passengers = 2;
sportscar.FuelCap = 14;
sportscar.Mpg = 12;
// Get the ranges.
range1 = minivan.Range();
range2 = sportscar.Range();

---------- Post added at 11:20 AM ---------- Previous post was at 11:17 AM ----------

Console.WriteLine("Minivan can carry " + minivan.Passengers +
" with range of " + range1 + " miles.");
Console.WriteLine("Sportscar can carry " + sportscar.Passengers +
" with range of " + range2 + " miles.");
}
}
The output is shown here:
Minivan can carry 7 with range of 336 miles.
Sportscar can carry 2 with range of 168 miles.
In the program, notice that when Range( ) is called, it is put on the right side of an
assignment statement. On the left is a variable that will receive the value returned by Range( ).
Thus, after this line executes,
range1 = minivan.Range();
the range of the minivan object is stored in range1.
Notice that Range( ) now has a return type of int. This means that it will return an integer
value to the caller. The return type of a method is important because the type of data returned

---------- Post added at 11:20 AM ---------- Previous post was at 11:20 AM ----------

by a method must be compatible with the return type specified by the method. Thus, if you
want a method to return data of type double, its return type must be type double.
Although the preceding program is correct, it is not written as efficiently as it could be.
Specifically, there is no need for the range1 or range2 variables. A call to Range( ) can be
used in the WriteLine( ) statement directly, as shown here:
Console.WriteLine("Minivan can carry " + minivan.Passengers +
" with range of " + minivan.Range() + " Miles");
In this case, when WriteLine( ) is executed, minivan.Range( ) is called automatically and its
value will be passed to WriteLine( ). Furthermore, you can use a call to Range( ) whenever
the range of a Vehicle object is needed. For example, this statement compares the ranges of
two vehicles:
if(v1.Range() > v2.Range()) Console.WriteLine("v1 has greater range");

---------- Post added at 11:37 AM ---------- Previous post was at 11:20 AM ----------

Q: I have heard that C# detects “unreachable code.” What does this mean?
A: You heard correctly. The C# compiler will issue a warning message if you create a method
that contains code that no path of execution will ever reach. Consider this example:
public void m() {
char a, b;
// ...
if(a==b) {
Console.WriteLine("equal");
return;
} else {
Console.WriteLine("not equal");
return;
}
Console.WriteLine("this is unreachable");
}
Here, the method m( ) will always return before the final WriteLine( ) statement is executed.
If you try to compile this method, you will receive a warning. In general, unreachable code
constitutes a mistake on your part, so it is a good idea to take unreachable code warnings
seriously!

---------- Post added at 11:38 AM ---------- Previous post was at 11:37 AM ----------

Use Parameters
It is possible to pass one or more values to a method when the method is called. As explained,
a value passed to a method is called an argument. Inside the method, the variable that receives
the argument is called a formal parameter, or just parameter, for short. Parameters are declared
inside the parentheses that follow the method’s name. The parameter declaration syntax is the
same as that used for variables. The scope of a parameter is the body of its method. Aside from
its special task of receiving an argument, it acts like any other local variable.
Here is a simple example that uses a parameter. Inside the ChkNum class, the method
IsEven( ) returns true if the value that it is passed is even. It returns false otherwise.
Therefore, IsEven( ) has a return type of bool.
// A simple example that uses a parameter.
using System;
// This class contains the method isEven that takes a parameter.
class ChkNum {
// Return true if x is even.
public bool IsEven(int x) {
if((x%2) == 0) return true;
else return false;
}
}

---------- Post added at 11:44 AM ---------- Previous post was at 11:38 AM ----------

class ParmDemo {
static void Main() {
ChkNum e = new ChkNum();
if(e.IsEven(10)) Console.WriteLine("10 is even.");
if(e.IsEven(9)) Console.WriteLine("9 is even.");
if(e.IsEven(8)) Console.WriteLine("8 is even.");
}
}
Here is the output produced by the program:
10 is even.
8 is even.
In the program, IsEven( ) is called three times, and each time, a different value is passed. Let’s
look at this process closely. First, notice how IsEven( ) is called. The argument is specified
between the parentheses. When IsEven( ) is called the first time, it is passed the value 10. Thus,
when IsEven( ) begins executing, the parameter x receives the value 10. In the second call, 9 is
the argument and x then has the value 9. In the third call, the argument is 8, which is the value

---------- Post added at 11:47 AM ---------- Previous post was at 11:44 AM ----------

that x receives. The point is that the value passed as an argument when IsEven( ) is called is
the value received by its parameter, x.
A method can have more than one parameter. Simply declare each parameter, separating
one from the next with a comma. For example, the Factor class defines a method called
IsFactor(( ) that determines if the first parameter is a factor of the second.
using System;
class Factor {
// Determine if x is a factor of y.
public bool IsFactor(int x, int y) {
if( (y % x) == 0) return true;
else return false;
}
}
class IsFact {
static void Main() {
Factor x = new Factor();
if(x.IsFactor(2, 20)) Console.WriteLine("2 is factor");
if(x.IsFactor(3, 20)) Console.WriteLine("this won't be displayed");
}
}
Notice that when IsFactor( ) is called, the arguments are also separated by commas.
When using multiple parameters, each parameter specifies its own type, which can differ
from the others. For example, this is perfectly valid:
int MyMeth(int a, double b, float c) {
// ...

---------- Post added at 11:49 AM ---------- Previous post was at 11:47 AM ----------

Add a Parameterized Method to Vehicle
You can use a parameterized method to add a new feature to the Vehicle class: the ability
to compute the amount of fuel needed for a given distance. This new method is called
FuelNeeded( ). This method takes the number of miles that you want to drive and returns the
number of gallons of gas required. The FuelNeeded( ) method is defined like this:
public double FuelNeeded(int miles) {
return (double) miles / Mpg;
}
Notice that this method returns a value of type double. This is useful, since the amount of fuel
needed for a given distance might not be an even number.

---------- Post added at 11:50 AM ---------- Previous post was at 11:49 AM ----------

The entire Vehicle class that includes FuelNeeded( ) is shown here:
/*
Add a parameterized method that computes the
fuel required for a given distance.
*/
using System;
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
// Return the range.
public int Range() {
return Mpg * FuelCap;
}
// Compute fuel needed for a given distance.
public double FuelNeeded(int miles) {
return (double) miles / Mpg;
}
}
class CompFuel {
static void Main() {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
double gallons;
int dist = 252;
// Assign values to fields in minivan.
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
// Assign values to fields in sportscar.
sportscar.Passengers = 2;
sportscar.FuelCap = 14;
sportscar.Mpg = 12;
gallons = minivan.FuelNeeded(dist);
Console.WriteLine("To go " + dist + " miles minivan needs " +
gallons + " gallons of fuel.");
gallons = sportscar.FuelNeeded(dist);

zied toumi
10-21-2013, 16:08
Console.WriteLine("To go " + dist + " miles sportscar needs " +
gallons + " gallons of fuel.");
}
}
The output from the program is shown here:
To go 252 miles minivan needs 12.0 gallons of fuel.
To go 252 miles sportscar needs 21.0 gallons of fuel.
Create a Help Class
If one were to try to summarize the essence of the class in a single sentence, it might be this:
A class encapsulates functionality. Of course, sometimes the trick is knowing where one
“functionality” ends and another begins. As a general rule, you will want your classes to be
the building blocks of your larger application. To do this, each class must represent a single
functional unit that performs clearly delineated actions. Thus, you will want your classes to
be as small as possible—but no smaller! That is, classes that contain extraneous functionality
confuse and destructure code, but classes that contain too little functionality are fragmented.
What is the balance? It is at this point that the science of programming becomes the art of
programming. Fortunately, most programmers find that this balancing act becomes easier
with experience.

---------- Post added at 12:10 PM ---------- Previous post was at 12:03 PM ----------

To begin gaining that experience, we will convert the help system developed in the Try
This sections in Chapter 3 into a help class. Let’s examine why this is a good idea. First, the
help system defines one logical unit. It simply displays the syntax for C#’s control statements.
Thus, its functionality is compact and well defined. Second, putting help in a class is an
aesthetically pleasing approach. Whenever you want to offer the help system to a user, simply
instantiate a help-system object. Finally, because help is encapsulated, it can be upgraded or
changed without causing unwanted side effects in the programs that use it.
Step by Step
1. Create a new file called HelpClassDemo.cs. To save you some typing, you might want to
copy the file from the final Try This section in Chapter 3, Help3.cs, into HelpClassDemo.cs.
2. To convert the help system into a class, you must first determine precisely what constitutes
the help system. For example, in Help3.cs, there is code to display a menu, input the
user’s choice, check for a valid response, and display information about the item selected.
The program also loops until a q is pressed. If you think about it, it is clear that the menu,
the check for a valid response, and the display of the information are integral to the help
system. How user input is obtained and whether repeated requests should be processed
are not integral. Thus, you will create a class that displays the help information and the
help menu and that checks for a valid selection. This functionality can be organized into
methods, called HelpOn( ), ShowMenu( ), and IsValid( ).

---------- Post added at 12:10 PM ---------- Previous post was at 12:10 PM ----------

3. Create the HelpOn( ) method, as shown here:
public void HelpOn(char what) {
switch(what) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;
case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case '6':
Console.WriteLine("The break:\n");
Console.WriteLine("break; or break label;");
break;
case '7':
Console.WriteLine("The continue:\n");
Console.WriteLine("continue; or continue label;");
break;
case '8':
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}

---------- Post added at 12:16 PM ---------- Previous post was at 12:10 PM ----------

4. Create the ShowMenu( ) method:
public void ShowMenu() {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while");
Console.WriteLine(" 6. break");
Console.WriteLine(" 7. continue");
Console.WriteLine(" 8. goto\n");
Console.Write("Choose one (q to quit): ");
}
5. Create the IsValid( ) method, shown here:
public bool IsValid(char ch) {
if(ch < '1' | ch > '8' & ch != 'q') return false;
else return true;
}

---------- Post added at 12:39 PM ---------- Previous post was at 12:16 PM ----------

6. Assemble the foregoing methods into the Help class, shown here:
class Help {
public void HelpOn(char what) {
switch(what) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;

---------- Post added at 12:43 PM ---------- Previous post was at 12:39 PM ----------

case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case '6':
Console.WriteLine("The break:\n");
Console.WriteLine("break; or break label;");
break;
case '7':
Console.WriteLine("The continue:\n");
Console.WriteLine("continue; or continue label;");
break;
case '8':
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}
public void ShowMenu() {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while");
Console.WriteLine(" 6. break");
Console.WriteLine(" 7. continue");
Console.WriteLine(" 8. goto\n");
Console.Write("Choose one (q to quit): ");
}

---------- Post added at 01:07 PM ---------- Previous post was at 12:43 PM ----------

public bool IsValid(char ch) {
if(ch < '1' | ch > '8' & ch != 'q') return false;
else return true;
}
}
7. Finally, create a class called HelpClassDemo.cs that uses the new Help class. Have Main( )
display help information until the user enters q. The entire listing for HelpClassDemo.cs is
shown here:
// The Help system from Chapter 3 converted into a Help class.
using System;

---------- Post added at 01:07 PM ---------- Previous post was at 01:07 PM ----------

class Help {
public void HelpOn(char what) {
switch(what) {
case '1':
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case '3':
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;

---------- Post added at 01:08 PM ---------- Previous post was at 01:07 PM ----------

case '5':
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case '6':
Console.WriteLine("The break:\n");
Console.WriteLine("break; or break label;");
break;
case '7':
Console.WriteLine("The continue:\n");
Console.WriteLine("continue; or continue label;");
break;
case '8':
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}

zied toumi
10-21-2013, 16:09
class HelpClassDemo {
static void Main() {
char choice;
Help hlpobj = new Help();
for(;;) {
do {
hlpobj.ShowMenu();
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( !hlpobj.IsValid(choice) );
if(choice == 'q') break;
Console.WriteLine("\n");
hlpobj.HelpOn(choice);
}
}
}
When you try the program, you will find that it is functionally the same as the final version
in Chapter 3. The advantage to this approach is that you now have a help system component
that can be reused whenever it is needed.
Constructors
In the preceding examples, the instance variables of each Vehicle object had to be set manually
using a sequence of statements, such as:
minivan.Passengers = 7;
minivan.FuelCap = 16;
minivan.Mpg = 21;
An approach like this would never be used in professionally written C# code. Aside from
being error-prone (you might forget to set one of the fields), there is simply a better way to
accomplish this task: the constructor.
A constructor initializes an object when it is created. It has the same name as its class and
is syntactically similar to a method. However, constructors have no explicit return type. The
general form of a constructor is shown here:
access class-name(param-list) {
// constructor code
}
Typically, you will use a constructor to give initial values to the instance variables defined by
the class, or to perform any other startup procedures required to create a fully formed object.
Often, access is public because a constructor is usually called from outside its class. The
parameter-list can be empty, or it can specify one or more parameters.
All classes have constructors, whether you define one or not, because C# automatically
provides a default constructor that causes all member variables to be initialized to their default
values. For most value types, the default value is zero. For bool, the default is false. For
reference types, the default is null. However, once you define your own constructor, the default
constructor is no longer used.
Here is a simple example that uses a constructor:

---------- Post added at 01:09 PM ---------- Previous post was at 01:08 PM ----------

// A simple constructor.
using System;
class MyClass {
public int x;
public MyClass() {
x = 10;
}
}
class ConsDemo {
static void Main() {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
Console.WriteLine(t1.x + " " + t2.x);
}
}
In this example, the constructor for MyClass is
public MyClass() {
x = 10;
}
Notice that the constructor is specified as public because the constructor will be called from
code defined outside of its class. As mentioned, most constructors are declared public for
this reason. This constructor assigns the instance variable x of MyClass the value 10. The
constructor is called by new when an object is created. For example, in the line
MyClass t1 = new MyClass();
the constructor MyClass( ) is called on the t1 object, giving t1.x the value 10. The same is
true for t2. After construction, t2.x has the value 10. Thus, the output from the program is
10 10

imaddine1986
10-21-2013, 16:13
اولا شكرا على الموضوع
اخي هذا المنتدى مختص باللغة العربية كان افضل ان تقدم ملخص بالغة العربية
وترد على استفسارات الاعضاء افضل او تكتفي بوضع الكتاب للتحميل

sondes
10-21-2013, 17:38
السلام علكم ورحمة الله وبركاته
هذا الموقع بلغة العربية ارجاء إستعمل هاته الاغ انها سهلا جدا وشكراً علي لتفاهم

zied toumi
10-23-2013, 01:43
Parameterized Constructors
In the preceding example, a parameterless constructor was used. While this is fine for some
situations, most often, you will need a constructor that accepts one or more parameters.
Parameters are added to a constructor in the same way that they are added to a method: Just
declare them inside the parentheses after the constructor’s name. For example, here, MyClass
is given a parameterized constructor:
// A parameterized constructor.
using System;
class MyClass {
public int x;
public MyClass(int i) {
x = i;
}
}
class ParmConsDemo {
static void Main() {
MyClass t1 = new MyClass(10);

---------- Post added at 03:14 PM ---------- Previous post was at 03:12 PM ----------

MyClass t2 = new MyClass(88);
Console.WriteLine(t1.x + " " + t2.x);
}
}
The output from this program is shown here:
10 88
In this version of the program, the MyClass( ) constructor defines one parameter called i,
which is used to initialize the instance variable, x. Thus, when this line executes,
MyClass t1 = new MyClass(10);
the value 10 is passed to i, which is then assigned to x.
Add a Constructor to the Vehicle Class
We can improve the Vehicle class by adding a constructor that automatically initializes the
Passengers, FuelCap, and Mpg fields when an object is constructed. *** special attention to
how Vehicle objects are created.
// Add a constructor to Vehicle.

---------- Post added at 04:00 PM ---------- Previous post was at 03:14 PM ----------

using System;
class Vehicle {
public int Passengers; // number of passengers
public int FuelCap; // fuel capacity in gallons
public int Mpg; // fuel consumption in miles per gallon
// This is a constructor for Vehicle.
public Vehicle(int p, int f, int m) {
Passengers = p;
FuelCap = f;
Mpg = m;
}
// Return the range.
public int Range() {
return Mpg * FuelCap;
}
// Compute fuel needed for a given distance.
public double FuelNeeded(int miles) {
return (double) miles / Mpg;
}
}

---------- Post added at 10:42 PM ---------- Previous post was at 04:00 PM ----------

Destructors
It is possible to define a method that will be called just prior to an object’s final destruction
by the garbage collector. This method is called a destructor, and it can be used in some highly
specialized situations to ensure that an object terminates cleanly. For example, you might use a
destructor to ensure that a system resource owned by an object is released. It must be stated at
the outset that destructors are a very advanced feature that are applicable to certain specialized
situations. They are not normally needed. However, because they are part of C#, they are
briefly described here for completeness.
Destructors have this general form:
~class-name( ) {
// destruction code
}
Here, class-name is the name of the class. Thus, a destructor is declared like a constructor,
except that it is preceded with a ~ (tilde). Notice it has no return type.
It is important to understand that the destructor is called just prior to garbage collection.
It is not called when a variable containing a reference to an object goes out of scope, for
example. (This differs from destructors in C++, which are called when an object goes out
of scope.) This means that you cannot know precisely when a destructor will be executed.
Furthermore, it is possible for your program to end before garbage collection occurs, so a
destructor might not get called at all.

---------- Post added at 10:43 PM ---------- Previous post was at 10:42 PM ----------

The this Keyword
Before concluding this chapter, it is necessary to introduce this. When a method is called, it is
automatically passed a reference to the invoking object (that is, the object on which the method
is called). This reference is called this. Therefore, this refers to the object on which the method
is acting. To understand this, first consider a program that creates a class called Pwr that
computes the result of a number raised to some integer power:
using System;
class Pwr {
public double b;
public int e;
public double val;
public Pwr(double num, int exp) {
b = num;
e = exp;
val = 1;
for( ; exp>0; exp--) val = val * b;
}

doola2020
10-23-2013, 02:58
طبعا الموضوع مية في المية منقول ولكن يا أخي إن أردت أن تنقل موضوع من الخارج حتي انقل موضوع يكون باللغة العربية عشان ميبانش مثلا

AshrafQassim
10-24-2013, 13:08
انا باقترح اقتراح وهو ان يكون التعلم ف البرمجة من خلال الفيديو وده تلاقه متوفر بكثر على اليوتيوب وبياخدك
من تحت الصفر حتى الاحتراف ومن اول ما تسطب البرنامج لغاية ما تصصم البرامج بنفسك

tijikawa
10-24-2013, 13:11
نعم يا اخي

WASEM DALLOUL
10-24-2013, 13:56
السلام عليكم
لغة السي لغة جيده جدا ومهمة جدا وهي تفيدنا في برمجة المؤشرات والأكسبيرتات التي نستخدمها في تجارتنا في الفوركس

nacero
12-22-2013, 17:00
حصل قطاع الصناعات الكيميائية على النصيب الأعلى من حيث عدد القروض المعتمدة له، إذ بلغ عددها سبعة قروض، يليه قطاع الصناعات الاستهلاكية بعدد أربعة قروض، ثم قطاعا الصناعات الهندسية وصناعة مواد البناء بواقع ثلاثة قروض لكل منهما، وأخيرا قطاع صناعة الأسمنت

Moro_Mezo
12-22-2013, 18:01
ان يكون التعلم ف البرمجة من خلال الفيديو وده تلاقه متوفر بكثر على اليوتيوب وبياخدك
من تحت الصفر حتى الاحتراف ومن اول ما تسطب البرنامج لغاية ما تصصم البرامج بنفسك

Marwan salem
12-22-2013, 18:02
السلام عليكم ورحمة الله وبركاته .اللهم صل وسلم على نبينا محمد
أخي الكريم أشكرك على موضوعك هذا وعلى هذا المجهود الجميل
وأتمنى أن تكون بخير أنت ولأعضاء الكرام ..........والسلام عليكم

انس بن تن
12-22-2013, 18:02
انك تتعلم ف البرمجه من خلال الفيديوهات
ودا اكيد بتلاقيه متوفره ع اليوتيوب من البدايه حتي الاحتراف بالتوووفيق للجميع

alimed
09-07-2015, 21:11
بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله تعالى وبركاته
بارك الله فيك على الموضوع القيم والمميز
جزاك الله الفردوس الاعلى

mamdouh.adil
09-13-2015, 20:03
يااااه كل ده كتاب ولا استفدت منه حاجه اولا يا سيدى الفاضل حجم المزكره هذه صغيره جدا وليس لها اى معنه
كان بامكانك تحميل الكتاب على اى موقع مجانى وترك الرابط هنا لتحميله ثانيا الكتاب العادى بيكون فيه مقدمه وتعريف بمحتوى الكتاب
وشرح مبسط ثم الماده العلميه ثم مثال عملى والخاتمه

MOHAMED2701
02-26-2016, 19:38
السلام عليكم
شكرا لك يا أخي الكريم على هذه المبادرة الجيده والمفيده وجزاك الله كل خير وبارك الله فيك
وارجو النجاح والتوفيق للجميع

TMT
03-11-2016, 03:23
السلام عليكم ورحمة الله وبركاته موضوع رائع يا اخى شرح موفق منك
مشكور علي هذه المعلومات الاكثر من رائعة
موضوع مميز وجيد وربنا يوفقك وبارك الله فيك وجزاك خيراً والسلام عليكم ورحمه الله

omar refaat
03-16-2016, 23:23
بسم الله الرحمن الرحيم
شكرا لك أخي العزيز علي هذه المشاركه الرائعه
وبفضلك قد فهمت ماينقصني اتمني ان يستفاد الاعضاء الاخرون من هذه المشاركة الرائعه مثلي
تقبل مروري
والسلام عليكم ورحمة الله وبركاته

Mohamed Ahmed Metawea
03-17-2016, 12:52
السلام عليكم ورحمه الله وبركاته
الله ينور عليك وياريت توضح اكتر وتمدنا بمعلومات مفيده اخري ولو ممكن شرح الفروق بين الحساب الاسلامي و العادي
وكيفيه سحب الاموال من المنتدي
جزالك الله خيرا
وشكرا

AboMoZo
03-19-2016, 10:26
بسم الله الرحمن الرحيم ... السلام عليكم ورحمة الله وبركاته
جزاك الله كل خير علي هذا الطرح المفيد و شكراً جزيلاً لك علي كل ما تقدمه
بإنتظار جديد مواضيعك فلا تحرمنا من الفائدة
تمنياتي بالتوفيق والسداد

ibrahimabdulsalam
03-19-2016, 10:27
اخي طرحك في هذاالموضوع انا متفق معه تماما لما فيه من معلومات قيمه
تفيد الفوركس وكل من يريد ان يتعلم بالتوفيق للجميع تقبل تحياتي وشكري صديقي العزيز ………

**TITO**
03-19-2016, 15:04
السلام عليكم ورحمة الله وبركاتة
شكرا لك يا اخى موضوع جيد جدا وجزاك الله خيرا بإنتظار جديد مواضيعك فلا تحرمنا من الفائدة
واحنا فى انتظار مواضيع منك تفيدنا اكتر مشكور وتقبل مرورى وتمنياتى ليك وللجميع بالتفوق

ashrafbek
04-13-2016, 09:14
اخي الكريم السلام عليكم ورحمة الله وبركاته
اشكرك جزيل الشكر علي موضوعك الاكثر من رائع
وانتظر منك ومن جميع اعضاء هذا المنتدي الجميل الاستفادة دوماً
تمنياتي للجميع بالتوفيق والنجاح

Mamdouh mahmoud
12-07-2019, 03:37
السلام عليكم ورحمة الله وبركاته
مشكور على المجهود الفائق فى الشرح المفصل
وفى انتظار المزيد .....
...*اشكرك*...
وتقبل مرورى ....

3omda00723
12-15-2019, 17:37
السلام عليكم ورحمه الله وبركاته
شكراا لك على هذه الافاده واتمنا مزيد من اعضاء المنتدى الكرام
شكراا