Sam visual csharp in 21 days

806 295 0
Sam visual csharp in 21 days

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Sam visual csharp in 21 days - tự học C#

800 East 96th St., Indianapolis, Indiana, 46240 USA Bradley L. Jones the C# Language in 21 Days Teach Yourself Sams Teach Yourself the C# Language in 21 Days Copyright © 2004 by Bradley L. Jones All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the pub- lisher. 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. International Standard Book Number: 0-672-32546-2 Library of Congress Catalog Card Number: 2003092624 Printed in the United States of America First Printing: July 2003 06 05 04 03 4 3 2 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. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages aris- ing from the information contained in this book. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quan- tity for bulk purchases or special sales. For more information, please contact: U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales +1-317-428-3341 international@pearsontechgroup.com A SSOCIATE P UBLISHER Michael Stephens E XECUTIVE E DITOR Candace Hall D EVELOPMENT E DITOR Mark Renfrow M ANAGING E DITOR Charlotte Clapp P ROJECT E DITOR Matthew Purcell C OPY E DITOR Krista Hansing I NDEXER Mandie Frank P ROOFREADER Paula Lowell T ECHNICAL E DITOR Anand Narayanaswamy T EAM C OORDINATOR Cindy Teeters I NTERIOR D ESIGNER Gary Adair C OVER D ESIGNER Alan Clements P AGE L AYOUT Michelle Mitchell Contents at a Glance Introduction 1 Week 1 Week at a Glance 5 D AY 1 Getting Started with C# 7 T YPE & R UN 1 37 2 Understanding C# Programs 41 3 Manipulating Values in Your Programs 83 4 Controlling Your Program’s Flow 117 T YPE & R UN 2 145 5 The Core of C# Programming: Classes 153 6 Packaging Functionality: Class Methods and Member Functions 179 7 Storing More Complex Stuff: Structures, Enumerators, and Arrays 211 Week 1 Week in Review 245 Week 2 Week at a Glance 269 D AY 8 Advanced Method Access 271 9 Handling Problems in Your Programs: Exceptions and Errors 303 T YPE & R UN 3 347 10 Reusing Existing Code with Inheritance 357 11 Formatting and Retrieving Information 395 12 Tapping into OOP: Interfaces 429 13 Making Your Programs React with Delegates, Events, and Indexers 449 14 Making Operators Do Your Bidding: Overloading 473 Week 2 Week in Review 499 Week 3 Week at a Glance 519 D AY 15 Using Existing Routines from the .NET Base Classes 521 16 Creating Windows Forms 553 17 Creating Windows Applications 591 T YPE & R UN 4 631 18 Working with Databases: ADO.NET 643 19 Creating Remote Procedures (Web Services) 661 T YPE & R UN 5 677 20 Creating Web Applications 687 21 A Day for Reflection and Attributes 705 Week 3 Week in Review 733 Appendices 721 A PPENDIX A C# Keywords 723 B Command-Line Compiler Flags for Microsoft’s Visual C# .NET 735 C Understanding Number Systems 741 D Installing and Using SharpDevelop 745 Index 751 On CD-ROM Answers Table of Contents Introduction 1 W EEK 1 At a Glance 5 C HAPTER 1 Getting Started with C# 7 What Is C#? 7 Preparing to Program 8 The Program-Development Cycle 9 Creating the Source Code 9 Understanding the Execution of a C# Program 11 Compiling C# Source Code to Intermediate Language 13 Completing the Development Cycle 14 Creating Your First C# Program 16 Entering and Compiling Hello.cs 17 Types of C# Programs 21 Creating Your First Window Application 21 Why C#? 25 C# Is Object-Oriented 26 C# Is Modular 26 C# Will Be Popular 26 A High-Level View of .NET 27 C# and Object-Oriented Programming (OOP) 28 Object-Oriented Concepts 28 Objects and Classes 30 Summary 31 Q&A 32 Workshop 33 Quiz 33 Exercises 34 T YPE & R UN 1 Numbering Your Listings 37 The First Type & Run 38 C HAPTER 2 Understanding C# Programs 41 Dissecting a C# Application 42 Starting with Comments 43 Basic Parts of a C# Application 48 Formatting with Whitespace 48 The Heart of C#: Keywords 49 vi Sams Teach Yourself the C# Language in 21 Days Literals 50 Identifiers 50 Exploring the Structure of a C# Application 50 Understanding C# Expressions and Statements 50 The Empty Statement 51 Analyzing Listing 2.1 51 Lines 1–4: Comments 51 Lines 5, 7, 13, 17, 21, and 23: Whitespace 51 Line 6—The using Statement 51 Line 8—Class Declaration 51 Lines 9, 11, 26, and 27: Punctuation Characters 51 Line 10: Main() 52 Lines 14–16: Declarations 52 Line 20: The Assignment Statement 52 Lines 24–25: Calling Functions 52 Storing Information with Variables 52 Storing Information in Variables 52 Naming Your Variables 53 Using Your Variables 55 Declaring a Variable 55 Assigning Values to Your Variables 56 Issues with Uninitialized Variables 58 Understanding Your Computer’s Memory 58 Introducing the C# Data Types 59 Numeric Variable Types 60 The Integral Data Types 62 Working with Floating-Point Values 69 Gaining Precision with Decimal 70 Storing Boolean Values 70 Working Checked Versus Unchecked Code 71 Data Types Simpler Than .NET 72 Literals Versus Variables 74 Working with Numeric Literals 74 Working with Boolean Literals ( true and false ) 75 Understanding String Literals 76 Creating Constants 76 A Peek at Reference Types 76 Summary 77 Q&A 78 Workshop 79 Quiz 79 Exercises 80 Contents vii C HAPTER 3 Manipulating Values in Your Programs 83 Displaying Basic Information 84 Displaying Additional Information 85 Manipulating Variable Values with Operators 87 Unary Operator Types 87 Binary Operator Types 88 Ternary Operator Types 88 Understanding Punctuators 88 Moving Values with the Assignment Operator 89 Working with Mathematical/Arithmetic Operators 90 Adding and Subtracting 90 Doing Multiplicative Operations 91 Working with the Compound Arithmetic Assignment Operators 93 Doing Unary Math 93 Making Comparisons with Relational Operators 96 Using the if Statement 96 Conditional Logical Operators 98 Understanding Logical Bitwise Operators 102 Understanding the Type Operators 102 Using the sizeof Operator 102 Shortcutting with the Conditional Operator 102 Understanding Operator Precedence 104 Changing Precedence Order 105 Converting Data Types 105 Understanding Operator Promotion 107 Bonus Material: For Those Brave Enough 107 Storing Variables in Memory 108 Understanding the Shift Operators 109 Manipulating Bits with Logical Operators 110 Flipping Bits with the Logical NOT Operator 113 Summary 114 Q&A 114 Workshop 114 Quiz 115 Exercises 115 C HAPTER 4 Controlling Your Program’s Flow 117 Controlling Program Flow 118 Using Selection Statements 118 Revisiting if 118 Discovering the switch Statement 123 viii Sams Teach Yourself the C# Language in 21 Days Using Iteration Statements 128 Executing Code with the while Statement 128 Working with the do Statement 132 Counting and More with the for Statement 134 The foreach Statement 139 Revisiting break and continue 139 Reviewing goto 139 Exploring Labeled Statements 140 Nesting Flow 141 Summary 142 Q&A 142 Workshop 143 Quiz 143 Exercises 143 T YPE & R UN 2 Guess the Number! 145 The Guess Type & Run 146 The WinGuess Type & Run 148 C HAPTER 5 The Core of C# Programming: Classes 153 Digging into Object-Oriented Programming 154 Encapsulation 154 Inheritance 155 Polymorphism 155 Reuse 156 Objects and Classes 156 Defining a Class 156 Declaring Classes 157 The Members of a Class 158 Working with Data Members, a.k.a. Fields 159 Accessing Data Members 159 Using Data Members 161 Using Classes as Data Members 163 Working with Nested Types 165 Using Static Variables 166 Inspecting the Application Class 168 Creating Properties 169 A First Look at Namespaces 172 Nested Namespaces 174 Summary 175 Q&A 175 Workshop 175 Quiz 176 Exercises 176 C HAPTER 6 Packaging Functionality: Class Methods and Member Functions 179 Getting Started with Methods 180 Using Methods 180 Understanding Program Flow with Method 183 Exploring the Format of a Method 183 The Method Header 184 Returning Data from a Method 184 Naming Methods 185 Building the Method Body 185 Passing Values to Methods 190 Working with Static Methods 192 Access Attributes for Parameters 192 Types of Class Methods 198 Property Accessor Methods 198 Constructors 198 Destructors/Finalizers 204 Summary 206 Q&A 207 Workshop 207 Quiz 208 Exercises 208 C HAPTER 7 Storing More Complex Stuff: Structures, Enumerators, and Arrays 211 Working with Structures 212 Understanding the Difference Between Structures and Classes 212 Structure Members 213 Nesting Structures 215 Structure Methods 216 Structure Constructors 218 Structure Destructors 220 Clarifying with Enumerators 220 Changing the Default Value of Enumerators 223 Changing the Underlying Type of an Enumerator 225 Contents ix

Ngày đăng: 06/08/2013, 17:48

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan