C# Programming for the Beginner
Welcome to C# and the world of Windows, Internet and World-Wide-Web
programming
with Visual Studio and the .NET platform! This book is the second in our
new .NET How
to Program series, which presents various leading-edge computing technologies
in the con-
database
video),
file
processing,
prepackaged
text of the .NET platform.
C# is the next phase in the evolution of C and C++ and was developed expressly
forMicrosoft’s .NET platform. C# provides the features that are most important to
program-mers,suchasobject-orientedprogramming,strings,graphics,graphical-user-inte
rface(GUI) components, exception handling, multithreading, multimedia (audio,
and
images, ani-mation
data
structures,
processing,Internet and World-Wide-Web-based client/server
networking and distributed computing.The language is appropriate for implementing
Internet- and World-Wide-Web-based appli-cations that seamlessly integrate with
PC-based applications. The .NET platform offers powerful capabilities for software
development and deploy-ment, including independence from a specific language or
platform. Rather than requiringdevelopers to learn a new programming language,
programmers can contribute to the samesoftware project, but write code using any (or
several) of the .NET languages (such as C#,Visual Basic .NET, Visual C++ .NET and
others) with which they are most competent. Inaddition
language
byenabling .NET
independence,
applications to reside on, and communicate across, multiple platforms—thus
facilitating the delivery of Web services over the Internet. The .NET platform
enablesWeb-based
consumer-electronic
cellPrefaceXXXIXphones and personal digital assistants, as
devices,
well as to desktop computers. The capabilities thatMicrosoft has incorporated into
the .NET platform create a new software-development par-adigm that will increase
applications
to
providing
portability
to
be
distributed
to
such
as
.NET
extends
program
programmer productivity and decrease development time.New Features in C# How to
ProgramThis edition contains many new features and enhancements, including:
Full-Color Presentation. This book is now in full color. Full color enables
readersto see sample outputs as they would appear on a color monitor. Also, we now
syn-tax color the C# code, similar to the way Visual Studio .NET colors the code in
itseditor window. Our syntax-coloring conventions are as follows:comments appear in
greenkeywords appear in dark blueliteral values appear in light bluetext, class,
method and variable names appear in blackerrors and ASP .NET directives appear in
red
“Code Washing.” This is our term for the process we use to format
the
programsin the book so that they have a carefully commented, open layout. The code
ap-pears in full color and is grouped into small, well-documented pieces. This
greatlyimproves code readability—an especially important goal for us, considering
thatthis book contains approximately 23,500 lines of code.
and
other
interactions
of reusable
Internet
protocols
using
standard
Web Services and ASP .NET. Microsoft’s .NET strategy embraces the
Internetand Web as integral to the software development and deployment processes.
Webservices—a key technology in this strategy—enables information sharing,
com-merce
and
technologies,such as HypertextTransfer Protocol (HTTP), Simple Objec tAccess
Protocol(SOAP) and Extensible Markup Language (XML). Web services enable
program-mers to package application functionality in a form that turns the Web into a
li-brary
.NET
and WebServices, we present a Web service that allows users to make airline seat
reserva-tions. In this example, a user accesses a Web page, chooses a seating option
andsubmits the page to the Web server. The page then calls a Web service that
checksseat availability. We also present
information related to Web services in
AppendixP, Crystal Reports® for Visual Studio® .NET, which discusses popular
reportingsoftware for database-intensive applications. Crystal Reports, which is
integratedinto Visual Studio .NET, provides the ability to expose a report as a Web
service.The appendix provides introductory information and directs readers to a
In Chapter
components.
software
21, ASP
walk-through of this process on the Crystal Decisions Web site
robust,
applications.
The
.NET
requests. To enable
scalable Web-based
interactive Web pages,
Web Forms, Web Controls and ASP .NET. Application developers must be ableto
platform
create
architecturesupports such applications. Microsoft’s .NET server-side
technology,
ActiveServer Pages (ASP) .NET, allows programmers to build Web documents that
re-spond to client
server-side
programsprocess information users input into HTML forms. ASP .NET is a significant
previous
from
de-XL Prefaceparture
allowing
program
developers
Web-based applications using the powerful
object-oriented languages of
.NET. ASP.NET also provides enhanced visual
programming capabilities, similar to thoseused inbuildingWindows forms for desktop
programs. Programmers can createWeb pages visually, by dragging and dropping Web
controls onto Web forms.Chapter 20, ASP .NET, Web Forms and Web Controls,
introduces these powerfultechnologies.
to
versions
of
ASP,
Object-Oriented Programming. Object-oriented programming is
the most
widelyemployed technique for developing robust, reusable software, and C# offers
en-hanced object-oriented programming features. This text offers a rich presentationof
object-oriented programming. Chapter 8, Object-Based Programming, intro-duces
how to create classes and objects. These concepts are extended in Chapter9,
Object-Oriented Programming: Inheritance, which discusses how programmerscan
create new classes that “absorb” the capabilities of existing classes. Chapter10,
Object-Oriented Programming: Polymorphism,
familiarizes the reader withthe
crucial concepts of polymorphism, abstract classes, concrete classes and inter-faces,
which facilitate powerful manipulations among objects belonging to an in-heritance
hierarchy.
XML. Use of Extensible Markup Language (XML)
is exploding in the
software-development industry, the e-business and e-commerce communities, and is
perva-sive throughout the .NET platform. Because XML is a platform-independent
tech-nology for describing data and for creating markup languages,
XML’s
dataportability integrates well with C#’s portable applications and services. Chapter18,
Extensible Markup Language (XML), introduces XML. In this chapter, we in-troduce
basic XML markup and discuss the technologies such as DTDs and Sche-ma, which
are used to validate XML documents’ contents. We also explain howto
programmatically manipulate XML documents using the Document ObjectModel
(DOM™) and how to transform XML documents into other types of doc-uments via
Extensible Stylesheet Language Transformations (XSLT).
such
as
files
printing
documents,
downloading
Multithreading. Computers enable us to perform many tasks in parallel (or
from a
con-currently),
andsurfing the Web. Multithreading is the technology through which
network
programmerscan develop applications that perform concurrent tasks. Historically, a
computerhas contained a single, expensive processor, which its operating system
wouldshare among all applications. Today, processors are becoming so inexpensive
thatit is possible to build affordable computers that contain many processors that
workin parallel—such computers are called multiprocessors. Multithreading is
effec-tive on both single-processor and multiprocessor systems. C#’s multithreading
ca-pabilities make the platform and its related technologies better prepared to dealwit
today’s
multimedia-intensive,database-intensive,network-based,
multiprocessor-based distributed applications. Chapter 14, Multithreading,provides a
detailed discussion of multithreading.
sophisticate
• ADO .NET. Databases store vast amounts of information that individuals and
or-
ganizations must access to conduct business. As an evolution of Microsoft's Ac-
tiveX Data Objects (ADO), ADO .NET represents a new approach for building
PrefaceXLIapplications that interact with databases. ADO .NET uses XML and
an enhancedobject model to provide developers with the tools they need to access and
manip-ulate databases
extensible, mission-critical multi-tier
applications.Chapter 19, Database, SQL and ADO .NET, details the capabilities of
ADO .NETand the Structured Query Language (SQL) to manipulate databases.
large-scale,
for
•
Visual Studio .NET Debugger. Debuggers are programs
that help
logic errors in program code. Visual Studio .NET
programmersfind and correct
contains a pow-erful debugging tool
that allows programmers to analyze their
programs line-by-line as those programs execute. In Appendix D, Visual Studio .NET
Debugger, weexplain how to use key debugger features, such as setting breakpoints
and “watch-es,” stepping into and out of procedures, and examining the procedure call
stack.
software
components
Integration. Prior
COM (Component Object Model)
called COM components, which
to the introduction
of .NET,many organizations spent tremendous amounts of time and money creating
reus-able
include
for Windows
ActiveX®controls and ActiveX DLLs
applications. InAppendix H, COM Integration, we discuss some of the tools available
in VisualStudio .NET for integrating these legacy components into .NET applications.
Thisintegration allows programmers to use existing sets of COM-based controls
with.NET components.
(dynamic link libraries)
If programmers document
XML Documentation. Documenting program code is crucial for software
devel-opment, because different programmers often work on an application during
thesoftware’s lifecycle, which usually includes multiple versions and can span
manyyears.
and methods, other
programmersworking on the application can learn and understand the logic underlying
the code,thus saving time and avoiding misunderstandings. To automate documenting
pro-grams, Visual Studio .NET provides an XML tool
for C# programmers.
AppendixE, XML Documentation, explains how a programmer can insert comments
in thecode, which produces a separate file providing the code documentation.
software
code
Career Opportunities. Appendix C, Career Opportunities,
introduces career
ser-vices available on the Internet. We explore online career services from both
theemployer’s and employee’s perspectives. We list many Web sites at which youcan
submit applications, search for jobs and review applicants (if you are interest-ed in
hiring someone). We also review services that build recruiting pages directlyinto
e-businesses. One of our reviewers told us that he had used the Internet as aprimary
tool in a recent job search, and that this appendix would have helped himexpand his
search dramatically.
Unicode. As computer systems evolved worldwide, computer vendors developed
numeric representations of character sets and special symbols for the local
lan-guages spoken in different countries. In some cases, different representations
weredeveloped for the same languages. Such disparate character sets hindered
commu-nication among computer systems. C# supports the Unicode Standard
(main-tained by a non-profit organizatio called the Unicode Consortium),
whichmaintains a single character set
that specifies unique numeric values for
charactersand special symbols in most of the world’s languages. Appendix G,
Unicode, dis-cusses the standard, overviews the Unicode Consortium Web site and
presents a C# application that displays “Welcome to Unicode!”in several languages.
XHTML. The World Wide Web Consortium (W3C) has declared HTML to be
alegacy technology that will undergo no further development. HTML is being
re-placed by the Extensibl Hypertext Markup Language (XHTML)—an XML-based
technology that is rapidly becoming the standard for describing Web con-tent. We use
XHTML in Chapter 18, Extensible Markup Language (XML), andoffer an
introduction to the technology in Appendix K, Introduction to XHTML:Part 1, and
Appendix L, Introduction to XHTML: Part 2. These appendices over-view headers,
images, lists, image maps and other features of this emerging mark-up language.
(We
also present a treatment of HTML in Appendices I and J,because ASP .NET,
used in Chapters 20 and 21, generates HTML content).
Accessibility. Although the World Wide Web has become an important part
ofmany people’s lives, the medium currently presents many challenges to peoplewith
disabilities. Individuals with hearing and visual impairments, in particular,World Wide
Web Consortium (W3C)
launched the Web Accessibil-ity Initiative (WAI),
whichprovides guidelines for making Web sites accessibleto people with disabilities.
Chapter 24, Accessibility, describes these guidelinesand highlightsvarious products
and services designed to improve the Web-brows-ing experiences of individuals with
disabilities. For example, the chapter introduc-es VoiceXML and CallXML—two
XML-based technologies for increasing theaccessibility of Web-based content for
people with visual impairments.Bit Manipulation.Computers work with data in the
form of binary digits, or bits,which can assume the values 1 or 0. Computer circuitry
performs various simplebit manipulations, such as examining the value of a bit,
setting the value of a bitand reversing a bit (from 1 to 0 or from 0 to 1). Operating
systems,
test-equipment,networking software and many other kinds of software
require that programs com-municate “directly with the hardware” by using bit
manipulation. Appendix O, BitManipulation, overviews
the bit manipulation
capabilities that the .NET Frame-work provides.
中文译文
初学者的 C#编程
欢迎 C #和世界上的 Windows ,互联网和万维网编程 与 Visual Studio
和.NET 平台!本书将在.NET 中介绍各种先进的计算技术的约束案文.NET 平台。
C #中的下一阶段发展过程中的 C 和 C + + ,并制定明确的微软的.NET 平台
C#中提供的功能,最重要的是程序 市场汇率,如面向对象编程,字符串,图形,
图形用户界面( GUI )的组成部分,例外处理,多线程,多媒体(音频,图像,
动画, 形成和视频) 文件处理,预先数据结构,数据库处理, 因特网和万维
网为基础的客户机/服务器网络和分布式计算。 语言是恰当的执行因特网和万维
网为基础的应用程序 阳离子的无缝集成的基于 PC 的应用,在.NET 平台提供了
强大的功能,软件开发和部署包括独立于特定的语言或平台。而不是要求 开发
人员能够学习一门新的编程语言,程序员可以促进同 软件项目,但是写代码使
用任何(或几个)的。 NET 语言(如 C # , Visual Basic 。 NET 中, Visual
C + +中的。 NET 和其他人) ,他们是最称职的。在 除了提供语言独立性, 。
NET 的可携性的扩展计划 有利的.NET 应用的居住权,并通报全国,多个平台,从
而有利于提供网络服务在互联网上。在.NET 平台使 基于 Web 的应用程序将分发
给消费者的电子设备,
手机和个人数字助理,以及台式电脑。的能力, 微软已经纳入了.NET 平台创建
一个新的软件开发标准杆 adigm ,提高编程效率和缩短开发时间。 新功能,在
C #中如何计划
这个版本包含了许多新功能和增强功能,其中包括:
•全彩色演示。这本书已全面颜色。全色彩使读者 看到样品产出,因为它们将出
现在彩色监视器。此外,我们现在综合征 税务色彩的 C #代码,类似于 Visual
Studio 。 NET 中颜色的代码 编辑器窗口。我们的语法着色公约如下:
评论出现在绿色 关键字是否出现在深蓝色 常值出现在浅蓝色 文本,类,
方法和变量名出现在黑色 错误和 ASP.NET 的指令出现在红
在长期的过程中,我们使用格式化程序在这本书,使他们有一个认真的评论,
开放布局。代码,并分成小的良好的记录片。这大大提高代码的可读性,一个特
别重要的目标对我们来说,考虑到这本书载有大约 23,500 行代码。
Web 服务和 ASP .NET 的。微软的.NET 战略拥抱互联网 和网络作为整体的软
件开发和部署过程。网络 服务的关键技术这一战略,使信息共享,沟通 商务及
其他互动使用标准的互联网协议和技术, 如超文本传输协议( HTTP ) ,简单
对象访问协议 和扩展标记语言( XML ) 。网络服务使节目 市场汇率来封装应
用功能的形式,把网络变成理 图书馆的可重用软件组件。ASP .NET 和网页服务
方面,我们提出一个 Web 服务,用户可以使飞机座位预订- 筹措。在这个例子中
用户访问一个网页选择座位选择,提交的网页到 Web 服务器。然后调用的网页的
Web 服务检查 座位可用性。我们还出席有关的信息网络服务于附录 磷的
Crystal Reports ®为 Visual Studio ® .NET 中,流行的报告,讨论 软件,数
据库密集型应用。水晶报表,这是综合 到 Visual Studio .NET 中,提供的能力,
揭露了一份报告作为一个 Web 服务。 附录提供了介绍性的信息和指示读者散步,
通过这一进程的 Crystal Decisions Web 站点。
Web 表单,Web 控件和 ASP .NET 的。应用开发者必须能够 建立强有力的,
可扩展的基于 Web 的应用程序。在.NET 平台架构 支持这种应用。微软的.NET
服务器端技术,主动 伺服器网页( ASP ).NET 中,允许程序员建立网页文件,
重新 spond 客户端请求。为了使互动网页,服务器端程序 处理信息的用户输入
到 HTML 表格。的 ASP.NET 中是一个重要的扫雷 仪 parture 从以前版本的 ASP
中,使开发人员计划网络 基于应用的强大的面向对象的语言的。 NET.ASP 技
术 .NET 中还提供了增强的可视化编程能力,类似于用 Windows 窗体的桌面程序。
程序员可以创造 网页视觉效果,通过拖放 Web 控件到 Web 窗体。ASP 技术.NET
中, Web 窗体和 Web 控件,介绍了这些功能强大的技术。面向对象编程。面向