Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Sharp Primer Plus
PREMIUM
Số trang
1176
Kích thước
10.8 MB
Định dạng
PDF
Lượt xem
1676

C Sharp Primer Plus

Nội dung xem thử

Mô tả chi tiết

only for RuBoard

C# Primer Plus

By Klaus Michelsen

Publisher : Sams Publishing

Pub Date : December 05, 2001

ISBN : 0-672-32152-1

Pages : 992

Copyright

ABOUT THE AUTHOR

ACKNOWLEDGMENTS

TELL US WHAT YOU THINK!

INTRODUCTION

Chapter 1. COMPUTERS AND COMPUTER PROGRAMMING: BASIC CONCEPTS

Chapter 2. YOUR FIRST C# PROGRAM

Chapter 3. A GUIDED TOUR THROUGH C#: PART I

Chapter 4. A GUIDED TOUR THROUGH C#: PART II

Chapter 5. YOUR FIRST OBJECT-ORIENTED C# PROGRAM

Chapter 6. TYPES PART I: THE SIMPLE TYPES

Chapter 7. TYPES PART II: OPERATORS, ENUMERATORS, AND STRINGS

Chapter 8. FLOW OF CONTROL PART I: BRANCHING STATEMENTS AND RELATED CONCEPTS

Chapter 9. FLOW OF CONTROL PART II: ITERATION STATEMENTS

Chapter 10. ARRAYS PART I: ARRAY ESSENTIALS

Chapter 11. ARRAYS PART II: MULTIDIMENSIONAL ARRAYS—SEARCHING AND SORTING ARRAYS

Chapter 12. CLASS ANATOMY PART I: static CLASS MEMBERS AND METHOD ADVENTURES

Chapter 13. CLASS ANATOMY PART II: OBJECT CREATION AND GARBAGE COLLECTION

Chapter 14. CLASS ANATOMY PART III: WRITING INTUITIVE CODE

Chapter 15. NAMESPACES, COMPILATION UNITS, AND ASSEMBLIES

Chapter 16. INHERITANCE PART I: BASIC CONCEPTS

Chapter 17. INHERITANCE PART II: abstract FUNCTIONS, POLYMORPHISM, and INTERFACES

Chapter 18. STRUCTS

Chapter 19. EXCEPTION HANDLING

Chapter 20. DELEGATES AND EVENTS

Chapter 21. PREPROCESSING, XML DOCUMENTATION, AND ATTRIBUTES

Chapter 22. FILE I/O BASICS

Chapter 23. RECURSION FUNDAMENTALS

Appendix A. ANSWERS TO QUIZZES AND EXERCISES

Index

only for RuBoard

C# Primer Plus

By Klaus Michelsen

Publisher : Sams Publishing

Pub Date : December 05, 2001

ISBN : 0-672-32152-1

Pages : 992

Copyright

ABOUT THE AUTHOR

ACKNOWLEDGMENTS

TELL US WHAT YOU THINK!

INTRODUCTION

Chapter 1. COMPUTERS AND COMPUTER PROGRAMMING: BASIC CONCEPTS

Chapter 2. YOUR FIRST C# PROGRAM

Chapter 3. A GUIDED TOUR THROUGH C#: PART I

Chapter 4. A GUIDED TOUR THROUGH C#: PART II

Chapter 5. YOUR FIRST OBJECT-ORIENTED C# PROGRAM

Chapter 6. TYPES PART I: THE SIMPLE TYPES

Chapter 7. TYPES PART II: OPERATORS, ENUMERATORS, AND STRINGS

Chapter 8. FLOW OF CONTROL PART I: BRANCHING STATEMENTS AND RELATED CONCEPTS

Chapter 9. FLOW OF CONTROL PART II: ITERATION STATEMENTS

Chapter 10. ARRAYS PART I: ARRAY ESSENTIALS

Chapter 11. ARRAYS PART II: MULTIDIMENSIONAL ARRAYS—SEARCHING AND SORTING ARRAYS

Chapter 12. CLASS ANATOMY PART I: static CLASS MEMBERS AND METHOD ADVENTURES

Chapter 13. CLASS ANATOMY PART II: OBJECT CREATION AND GARBAGE COLLECTION

Chapter 14. CLASS ANATOMY PART III: WRITING INTUITIVE CODE

Chapter 15. NAMESPACES, COMPILATION UNITS, AND ASSEMBLIES

Chapter 16. INHERITANCE PART I: BASIC CONCEPTS

Chapter 17. INHERITANCE PART II: abstract FUNCTIONS, POLYMORPHISM, and INTERFACES

Chapter 18. STRUCTS

Chapter 19. EXCEPTION HANDLING

Chapter 20. DELEGATES AND EVENTS

Chapter 21. PREPROCESSING, XML DOCUMENTATION, AND ATTRIBUTES

Chapter 22. FILE I/O BASICS

Chapter 23. RECURSION FUNDAMENTALS

Appendix A. ANSWERS TO QUIZZES AND EXERCISES

Index

only for RuBoard

only for RuBoard

C# Primer Plus

By Klaus Michelsen

Publisher : Sams Publishing

Pub Date : December 05, 2001

ISBN : 0-672-32152-1

Pages : 992

C# Primer Plus teaches the C# programming language and relevant parts of the .NET platform from the

ground up, walking you through the basics of object-oriented programming, important programming

techniques and problem solving while providing a thorough coverage of C#'s essential elements - such as

classes, objects, data types, loops, branching statements, arrays, and namespaces.

In early chapters guided tours take you sightseeing to the main attractions of C# and provide a fast

learning-path that enables you to quickly write simple C# programs. Your initial programming skills are

then gradually expanded, through the many examples, case studies, illustrations, review questions and

programming exercises, to include powerful concepts - like inheritance, polymorphism, interfaces and

exception handling, along with C#'s most innovative features - such as properties, indexers, delegates and

events.

With C# Primer Plus's dual emphasis on C# as well as fundamental programming techniques, this friendly

tutorial will soon make you a proficient C# programmer building Windows applications on the .NET platform.

only for RuBoard

only for RuBoard

Copyright

Copyright © 2002 by Sams Publishing

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by

any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from

the publisher. No patent liability is assumed with respect to the use of the information contained herein.

Although every precaution has been taken in the preparation of this book, the publisher and author assume

no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of

the information contained herein.

Library of Congress Catalog Card Number: 2001089509

Printed in the United States of America

First Printing: November 2001

04 03 02 01 4 3 2 1

Trademarks

All terms mentioned in this book that are known to be trademarks or service marks have been appropriately

capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book

should not be regarded as affecting the validity of any trademark or service mark.

Warning and Disclaimer

Every effort has been made to make this book as complete and as accurate as possible, but no warranty or

fitness is implied. The information provided is on an "as is" basis.

ASSOCIATE PUBLISHER

Jeff Koch

ACQUISITIONS EDITOR

Neil Rowe

DEVELOPMENT EDITOR

Kevin Howard

MANAGING EDITOR

Matt Purcell

PROJECT EDITOR

George E. Nedeff

COPY EDITOR

Pat Kinyon

INDEXER

Rebecca Salerno

PROOFREADER

Plan-It Publishing

TECHNICAL EDITOR

Brad Shannon

TEAM COORDINATOR

Denni Bannister

MEDIA DEVELOPER

Dan Scherf

INTERIOR DESIGNER

Gary Designer

COVER DESIGNER

Alan Clements

PAGE LAYOUT

Plan-It Publishing

Brad Lenser

DEDICATION

To Deborah,

My friend, my love, my wife.

only for RuBoard

only for RuBoard

ABOUT THE AUTHOR

Klaus Michelsen has a Masters degree in Economics from Aarhus University in Denmark. His studies

included artificial intelligence and courses in advanced computer science at Sydney University in Australia.

Since his first programming assignment for a shoe shop when he was 18, he has, during his 15 years of

programming experience, been exposed to many different technologies and programming languages,

including Java™, Visual J++, C++, MS Access, VB, Eiffel, and Pascal. Klaus has been working with C# and

.NET since their introduction in the year 2000.

only for RuBoard

only for RuBoard

ACKNOWLEDGMENTS

I would like to thank my wife Deborah who, despite my obstinate tendency to turn into a hermit during this

project and my countless late nights of writing frenzies, is still putting up with me. Without you and your

boundless support, this book project would have been impossible. Without you and our young boy Julian,

any project in my life would seem meaningless. I love you!

I would like to thank the team of editors, coordinators, and technicians at Sams Publishing for putting their

hard work and invaluable expertise into this project. I'm especially grateful to Neil Rowe, Acquisitions

Editor, for initially contacting me, encouraging me to engage in this project, and for his solid support during

the whole process. Thanks for taking a chance with a new writer; it has changed my life forever.

I would like to thank my mother Vita Lund Michelsen, for her strong support and incessant encouragement

throughout my life and in particular during the months I was writing. Thanks to my sister Kirsten for

keeping the spirits up on the other side of the world, even during difficult times; this gave me peace of

mind to focus on the book.

Lastly, thanks to my two dogs, Charlie and Honey, and the great composers Mozart and Beethoven who all

kept me company on many a late night.

only for RuBoard

only for RuBoard

TELL US WHAT YOU THINK!

As the reader of this book, you are our most important critic and commentator. We value your opinion and

want to know what we're doing right, what we could do better, what areas you'd like to see us publish in,

and any other words of wisdom you're willing to pass our way.

As an Associate Publisher for Sams Publishing, I welcome your comments. You can fax, email, or write me

directly to let me know what you did or didn't like about this book—as well as what we can do to make our

books stronger.

Please note that I cannot help you with technical problems related to the topic of this book, and that due to

the high volume of mail I receive, I might not be able to reply to every message.

When you write, please be sure to include this book's title and author as well as your name and phone or

fax number. I will carefully review your comments and share them with the author and editors who worked

on the book.

Fax: 317-581-4770

Email: [email protected]

Mail: Jeff Koch

Associate Publisher

Sams Publishing

201 West 103rd Street

Indianapolis, IN 46290 USA

only for RuBoard

only for RuBoard

INTRODUCTION

Philosophy, Goals, and Audience

How This Book Is Organized

Conventions

C# On The Internet

Contacting the Author

only for RuBoard

only for RuBoard

Philosophy, Goals, and Audience

Forming a group of grammatically correct sentences in the English language by combining verbs, nouns,

and other language elements is relatively easy compared to the task of constructing sentences that

together form a spellbinding, classical novel.

C#, like any other programming language, also consists of a basic set of language elements. To form valid

C# "sentences," these elements must be put together according to C#'s rules of syntax. This is relatively

easy compared to the task of creating a group of C# "sentences" that together form a successful, robust,

and bug-free program.

Not only does this book teach you C#'s language elements from the ground up, it also explains their

optimal use and shows you how they are combined to form robust and valuable programs. The latter is

achieved by including proven fundamental programming techniques (with an emphasis on object-oriented

programming) as part of the introduction of the C# language. To maintain this dual focus and to avoid any

superfluous distractions, only the parts of .NET directly relevant to C# are discussed. Accordingly, this book

is not about .NET's ASP+, Web Services, or Win Forms but about making you a proficient C# programmer.

This book is targeted at the beginner and requires no mathematical skills other than some simple algebra.

An absolute beginner should read the book from cover to cover.

If you've had limited experience with another programming language, you can still benefit from this book.

In that case, you might want to concentrate only on the .NET and C# related parts in Chapters 1 and 2 and

skip the discussions about abstraction and encapsulation (if your experience include object-oriented

programming) in Chapter 3.

Learning a new programming language is, in my view, best facilitated by combining several different

teaching tools, so that any one aspect can be viewed and attacked from different angles. Consequently, this

tutorial not only contains the basic text describing the various C# elements but also a generous number of

figures, source code examples (with accompanying sample output and analysis), case studies, Note boxes,

Tip boxes, Common Pitfall boxes, Syntax boxes, and review questions (with answers located in Appendix A,

"Answers to Quizzes and Exercises").

It is important to realize that learning to program is not about learning a lot of dry theory and concepts by

heart. Instead, it is about experimenting, learning from mistakes, unleashing your creativity, and having

fun. As a result, each chapter ends with a set of programming exercises with which you can experiment,

test, and improve your skills.

only for RuBoard

only for RuBoard

How This Book Is Organized

This book is divided into 23 chapters and 6 appendixes. Five appendixes (Appendix B–F) are located on

SAMS Web site at www.samspublishing.com.

Chapter 1: "Computers and Computer Programming: Basic Concepts" starts with an overview of the

fundamental computer hardware and software concepts required to begin learning C# and computer

programming. The last part of the chapter introduces .NET and highlights important services provided by

this essential C# programming platform.

Chapter 2: "Your First C# Program" introduces you to the basic software development phases that

successful programmers go through to produce robust computer programs and gives you an initial feel for

object-oriented programming. The chapter culminates by showing you the mechanics of writing, compiling,

and running a simple C# program.

Chapters 3 and 4: Often, an enjoyable and efficient way to learn about an unknown location you visit for

the first time is to join a guided tour. Chapter 3, "A Guided Tour Through C#: Part I," and Chapter 4, "A

Guided Tour Through C#: Part II," take you to the main attractions of the C# language needed to write

simple C# programs. Rather than getting bogged down by details and theory, the aim in these two

chapters is to kick start your ability to write simple C# programs and allow you to play and experiment with

the language. This is achieved by letting the contents of a few simple C# programs dictate the hands-on

and fast-paced story line of these two chapters.

Chapter 5: The first part of Chapter 5, "Your First Object-Oriented Program," builds on the practical

knowledge you acquired from Chapters 3 and 4 to give you a deeper insight into the basic structure of the

C# language. The second part elaborates on the object-oriented programming knowledge you have gained

up until now and turns it into an object-oriented elevator simulation program.

Chapter 6: By now, you will know how to write simple object-oriented C# programs. However, many

aspects of the C# language have intentionally been ignored to keep the fast track of the previous chapters

free from obscuring and distracting elements. Chapter 6, "Types Part I: The Simple Types," and the

remaining chapters of the book are devoted to letting you expand the basic kinds of C# programs you have

mastered in the first five chapters. This is done through a systematic and thorough discussion of the many

exciting and powerful elements in C# that you now have enough knowledge to appreciate, absorb, and use

in the programs with which you might already be experimenting and playing.

Chapter 6 discusses the type concept in general and the simple types, used in most C# programs, in

particular. It also introduces an important notation form that is used throughout the rest of the book to

describe the syntax of the various C# elements.

Chapter 7: Types allow you to specify attributes about the data kept in a program. Operators in general act

on these data. Without operators, program data remain motionless and in most cases useless. Chapter 7,

"Types Part II: Operators, Enumerators, and Strings," provides an overview of the different kinds of

operators found in C# and looks closer at the main arithmetic operators. Furthermore, the chapter

discusses the derived types—string (used to represent text) and enum (used to handle unchanging

values). Several useful and interesting string-related program examples are presented and discussed.

Chapter 8: Branching statements allow programs to behave intelligently by reacting differently to different

circumstances. Chapter 8, "Flow of Control Part I: Branching Statements and Related Concepts," contains

elaborate discussions of the entire set of C# branching statement constructs—if, if-else, and

switch—along with their close associates—comparison and logical operators—that also are pivotal for

implementing the iteration statements presented in Chapter 9.

Chapter 9: Iteration statements are used to perform the same actions over and over again until some

condition is no longer true. C#'s iteration statements, consisting of the while loop, the do-while loop, and

the for loop, are discussed in Chapter 9, "Flow of Control Part II: Iteration Statements." Their treatment is

elaborated by introducing the useful idea of nested iteration statements. Finally, the structured

programming concept that contains proven principles about how to construct robust software on the very

detailed design level is presented.

Chapter 10: An array is used to represent a small or large group of similar data items. Chapter 10, "Arrays

Part I: Array Essentials," presents arrays and shows how they can be accessed efficiently by using the

iteration statements presented in Chapter 9. It also introduces the foreach iteration statement specifically

targeted at arrays and other collections of data items. The array is a reference type—an important object￾oriented concept. For that reason, arrays are used as examples in this chapter for teaching about reference

types.

Chapter 11: Chapter 10 only deals with one-dimensional arrays designed for storing data elements that can

be viewed as sitting on a line one after the other. Sometimes, data are better represented in two￾dimensional tables. Our ability to represent these types of data with two-dimensional arrays is discussed in

Chapter 11, "Arrays Part II: Multidimensional Arrays," together with arrays of three or more dimensions.

The symbiotic relationship between multidimensional arrays and nested loops is a great example of how

data structures and program logic go hand in hand. The chapter concludes by describing a couple famous

algorithms for sorting and searching arrays and shows examples of how you can save countless

programming hours by reusing the pre-built program parts found in .NET.

Chapter 12: Classes and objects are at the core of object-oriented programming. Classes are written by the

programmer in the source code and act as blueprints for objects generated while the program is running.

Objects collaborate to provide the functionality of a C# program. The first part of Chapter 12, "Class

Anatomy Part I: static Class Members and Method Adventures," provides an overview of the ingredients

that make up a class. The chapter then continues, as do Chapters 13 and 14, with a detailed look at each of

those ingredients. Initially, the chapter looks at static class members that belong to a class rather than any

particular object. This is followed by a closer look at method overloading—a mechanism used to write

different versions of methods all of same name but with different interfaces and implementations.

Chapter 13: "Class Anatomy Part II: Object Creation and Garbage Collection" looks at how new objects are

initialized and old useless objects are disposed of. The latter is achieved through a built-in mechanism

called garbage collection that automatically frees the memory (needed by the newly-created objects of the

program) otherwise occupied by the useless objects.

Chapter 14: Properties, indexers and user-defined operator overloading are among the C# elements

presented in Chapter 14, "Class Anatomy Part III: Writing Intuitive Code." They can, if used correctly,

make the source code appear simpler and more intuitive.

Chapter 15: Namespaces assist you in organizing your classes while you write the source code as well as in

keeping the classes easily accessible for other programmers when they reuse your classes. Chapter 15,

"Namespaces, Compilation Units, and Assemblies," discusses namespaces, compilation units, and

assemblies. It also demonstrates the flexibility by which they can be combined and shows you how their

overall layout can be tailor-made to suit many different application configurations.

Chapter 16: Inheritance allows you to derive class A from class B and thereby let class A automatically

inherit the features of class B. Features specific to class A can then be added manually in the code.

Inheritance is a core concept in object-oriented programming with many virtues and is discussed at length

in Chapter 16, "Inheritance Part I: Basic Concepts."

Chapter 17: "Inheritance Part II: abstract Functions, Polymorphism, and Interfaces," discusses

polymorphism and how it allows you to program on a high abstraction level by letting you program against

a few simple interfaces while the program takes care of the underlying complexities. Polymorphism is made

possible by inheritance and abstract functions. Interfaces are also discussed in this chapter; they allow you

to implement polymorphism across the rigid structures of one or more inheritance hierarchies.

Chapter 18: "Structs" looks at structs, which are lightweight alternatives to their close cousins—classes.

Chapter 19: An exception is an abnormal condition in your program and causes a special exception object

to be generated by .NET during the execution of the troubled program. Chapter 19, "Exception Handling,"

introduces exception handling and shows you how to avoid abrupt user-unfriendly program terminations,

among other things.

Chapter 20: Conventionally, you must decide which actions a program will execute while you are writing the

source code. Delegates allow you to postpone those decisions until the program is running. This allows your

programs to be more dynamic with the ability to adjust to a variety of situations. Chapter 20, "Delegates

and Events," discuss delegates and their relationship to events. Delegates form the basis for events that,

through event-driven programming, have become an important part of many modern application types,

especially graphical user interfaces (GUIs).

Chapter 21: "Preprocessing, XML Documentation and Attributes" explains how to conveniently exclude parts

of your source code from being included in the finished program through the use of preprocessor directives.

This is useful when you want to generate different application versions from the same source code. The

chapter continues by looking at C#'s ability to extract XML-formatted source code documentation

automatically and directly from the comments you insert into your source code. The chapter ends by

presenting attributes and their ability to let you add additional declarative information to C#'s code

elements.

Chapter 22: Files let you store data permanently on a disk. They allow programs to read them repeatedly

and can be sent over networks to remote computers. Chapter 22, "File I/O Basics," contains a brief

introduction to file I/O, which is short for file input (file flowing into a program) and file output (file flowing

out of a program).

Chapter 23: When a method asks itself to be executed it is called a recursive method. The use of recursive

methods is called recursion and this is covered in Chapter 23, "Recursion Fundamentals." Superficially,

recursion seems to represent circular, impossible, and useless logic. However, as this chapter demonstrates

through meticulous explanations, the recursion concept can be used to solve important computational

problems.

Appendix A: "Answers to Review Questions and Exercises" contains the answers to the review questions

and the programming exercises posed at the end of each chapter.

Appendix B: "Operator Precedence"

Appendix C: "Reserved Words In C#"

Appendix D: "Number Systems" discusses the binary, octal and hexadecimal number systems.

Appendix E: "Unicode Character Set"

Appendix F: "Using DOS Commands In The Console Window"

only for RuBoard

only for RuBoard

Conventions

To help you differentiate between different types of information presented in this book, we have used

different text styles and layout.

Important words and new terminology appear as Important words.

Text written onscreen by the computer looks as follows:

I'm your trusted computer

Text written onscreen and typed by you looks as follows:

Hello computer, are you there?

While writing text onscreen special keys pressed by you, such as the enter key, are symbolized by

<enter>.

Code shown as part of code examples has the following appearance:

private static void Main()

The line continuation symbol is positioned at the beginning of a source code line and indicates that

the preceding line of code was too long to fit the page. The previous line and the line following

should be regarded as one line.

only for RuBoard

Tải ngay đi em, còn do dự, trời tối mất!