Learning C# 3.0 pdf

694 6.9K 5
Learning C# 3.0 pdf

Đ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

www.it-ebooks.info www.it-ebooks.info www.it-ebooks.info Learning C# 3.0 www.it-ebooks.info Other resources from O’Reilly Related titles oreilly.com C# 3.0 Cookbook™ C# 3.0 Design Patterns C# 3.0 in a Nutshell Programming ASP.NET 3.5 Programming C# 3.0 Programming NET 3.5 Programming WCF Services Programming WPF oreilly.com is more than a complete catalog of O’Reilly books You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, programming languages, and operating systems Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries We specialize in documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches Visit conferences.oreilly.com for our upcoming events Safari Bookshelf (safari.oreilly.com) is the premier online reference library for programmers and IT professionals Conduct searches across more than 1,000 books Subscribers can zero in on answers to time-critical questions in a matter of seconds Read the books on your Bookshelf from cover to cover or simply flip to the page you need Try it today for free www.it-ebooks.info Learning C# 3.0 Jesse Liberty and Brian MacDonald Beijing • Cambridge • Farnham • Kưln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Learning C# 3.0 by Jesse Liberty and Brian MacDonald Copyright © 2009 Jesse Liberty and Brian MacDonald All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Editor: John Osborn Production Editor: Sumita Mukherji Copyeditor: Audrey Doyle Proofreader: Sada Preisch Indexer: Angela Howard Interior Designer: David Futato Cover Illustrator: Karen Montgomery Illustrator: Jessamyn Read Printing History: November 2008: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Learning C# 3.0, the image of a butterflyfish, and related trade dress are trademarks of O’Reilly Media, Inc Java™ is a trademark of Sun Microsystems, Inc .NET is a registered trademark of Microsoft Corporation Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein This book uses RepKover™ a durable and flexible lay-flat binding , ISBN: 978-0-596-52106-6 [M] www.it-ebooks.info Table of Contents Preface xv C# and NET Programming Installing C# Express C# 3.0 and NET 3.5 The NET Platform The NET Framework The C# Language Your First Program: Hello World The Compiler Examining Your First Program The Integrated Development Environment Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercise 4 10 11 16 17 18 19 Visual Studio 2008 and C# Express 2008 20 Before You Read Further The Start Page Projects and Solutions Project Types Templates Inside the Integrated Development Environment Building and Running Applications 22 22 22 24 25 26 28 v www.it-ebooks.info Menus The File Menu The Edit Menu The View Menu The Refactor Menu The Project Menu The Build Menu The Debug Menu The Data Menu The Format Menu The Tools Menu 29 29 30 36 41 41 41 41 41 42 42 The Window Menu The Help Menu Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises 43 44 44 45 45 C# Language Fundamentals 46 Statements Types Numeric Types Nonnumeric Types: char and bool Types and Compiler Errors WriteLine( ) and Output Variables and Assignment Definite Assignment Implicitly Typed Variables Casting Constants Literal Constants Symbolic Constants Enumerations Strings Whitespace Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises vi | Table of Contents 46 47 48 49 50 51 52 54 55 56 58 58 58 60 63 63 64 65 66 www.it-ebooks.info Operators 68 Expressions The Assignment Operator (=) Mathematical Operators Simple Arithmetic Operators (+, –, *, /) The Modulus Operator (%) Increment and Decrement Operators The Calculate and Reassign Operators Increment or Decrement by The Prefix and Postfix Operators Relational Operators Logical Operators and Conditionals The Conditional Operator Operator Precedence Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises 68 69 69 70 71 72 72 73 73 75 77 78 79 81 82 83 Branching 85 Unconditional Branching Statements Conditional Branching Statements if Statements Single-Statement if Blocks Short-Circuit Evaluation if else Statements Nested if Statements switch Statements Fall-Through and Jump-to Cases Switch on string Statements ReadLine( ) and Input Iteration (Looping) Statements Creating Loops with goto The while Loop The while Loop The for Loop Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises 86 88 88 90 92 94 95 98 101 102 103 104 105 106 108 109 118 119 120 Table of Contents | vii www.it-ebooks.info Object-Oriented Programming 121 Creating Models Classes and Objects Defining a Class Class Relationships The Three Pillars of Object-Oriented Programming Encapsulation Specialization Polymorphism Object-Oriented Analysis and Design Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises 123 123 124 125 126 126 127 128 129 130 131 131 Classes and Objects 133 Defining Classes Instantiating Objects Creating a Box Class Access Modifiers Method Arguments Return Types Constructors Initializers Object Initializers Anonymous Types The this Keyword Static and Instance Members Invoking Static Methods Using Static Fields Finalizing Objects Memory Allocation: The Stack Versus the Heap Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercises 134 135 137 138 139 141 142 144 146 146 147 148 149 151 154 155 161 162 163 Inside Methods 165 Overloading Methods viii | Table of Contents 165 www.it-ebooks.info debugging breakpoints, setting, 181–184 call stack, examining, 189 examining values, 184–188 stepping into code, 182 stopping, 190 watching specific values, 188–189 decimal type (C#), 47 Decimal type (.NET), 47 declarative referential integrity (DRI), 482 decoupling, 169 decrement by operator (––), 73–75 decrement operators, 72–75, 80 dedicated catch blocks, 366–368 default constructors, 142 default keyword, in switch statement, 99 definite assignment, 54, 177 delegate keyword, 381 delegates, 380, 381–385 events implemented with, 387–394 multicast delegates, 390 restricting to event handling, 394–399 Dequeue( ) method, queues, 320 derived classes, 222, 225 Design Patterns (Gamma et al.), 386 design time, 50 #develop compiler, 16 development teams, size of, 130 Device Emulator Manager, Tools menu, 42 devices, connecting to, 42 dictionaries, 325–328 DirectoryInfo class, 418 disconnected data architecture, 477 Dispose( ) method, 154 division operator (/), 70 division self-assignment operator (/=), 73 dll file extension, 23 while loops, 108 dockable windows, in C# Express, 27 Document class example, 263, 267, 271–276, 277, 280–285, 286 dot operator (.), 12 double quotes (" "), enclosing strings, 63, 332 double type (C#), 47, 49 Double type (.NET), 47 DoubleAnimation element, XAML, 457, 465 DRI (declarative referential integrity), 482 dynamic link libraries, 23 654 | Index E Edit menu, 30–36 editor in C# Express, 26–28 else if idiom, 96 else statement, 94, 96 emulators, connecting to, 42 encapsulating methods (see delegates) encapsulation, 126, 133 EndsWith( ) method, strings, 343 Enqueue( ) method, queues, 320 EnsureCapacity( ) method, StringBuilder, 349 enum keyword, 61 enumerations, 60–63 equals operator (==), 76, 339 equals sign (=), assignment operator, 53, 68, 69, 80, 337 equals sign, double (==), equals operator, 76, 339 equals sign, right angle bracket (=>), lambda operator, 400 Equals( ) method Object, 238, 249 strings, 334, 339 Error List window, 37 errors compared to exceptions, 358 compiler, 50 user causing, 358 escape characters, 52, 332 event handlers, 387 event keyword, 395–399 EventArgs class, 387 event-driven programming, 380 events (C#), 381, 385 for controls, 408–411 handling, 387 implementing with delegates, 387–394 inside WPF application, 470 publishing, 386 restricting delegates to event handling, 394–399 searching for, 31 subscribing to, 386 events (WPF) handling (see triggers) types of, 453 Events window, Visual Studio, 408–410 EventTrigger element, XAML, 454, 458, 465 www.it-ebooks.info examples Book class, 493–496, 497, 499, 500–505 Box class, 137, 166, 169 Clock class, 387–394, 396–399 complexity of, 128 Document class, 263, 267, 271–276, 277, 280–285, 286 FileCopier application, 411–414 Button events, handling, 426–431 source code for, 431–439 TreeView controls, populating, 415–422 TreeView events, handling, 422–426 Fraction class, 243–248, 254–257 Hello World, 6–9, 11–16 Hello WPF, 443–447 ListBox control, 294–298, 299–302, 303–307 MediaStorage class, 381–385 Northwind database, 486, 506–508, 509–512 presidents’ images, 460–473 source code for, xx using, permission for, xxi exception class, custom, 374–376 Exception class, System, 358, 370–374 exception handlers (see catch blocks) exceptions, 357 compared to bugs and errors, 358 custom, 374–376 finally blocks, 368–370 handling (see catch blocks) help file link for, providing, 371 message about, providing, 371 stack trace for, 371–374 throwing (raising), 357, 358, 359–361 types of, 358 exclamation point (!), not operator, 77 exclamation point, equals sign (!=), not equals operator, 76 exe file extension, 10, 23 executable programs, 23 Exists( ) method, lists, 308 explicit conversions, 57, 253 explicit keyword, 254, 256 Expression Blend tool, 452 expressions, 68 expressions, regular (see regular expressions) eXtensible Application Markup Language (XAML), 442 (see also WPF (Windows Presentation Foundation)) eXtensible Markup Language (XML), 444 External Tools, Tools menu, 43 F F10 key, in debugger, 182 F11 key, in debugger, 182 factoring out commonalities, 220, 415 fall-through cases, in switch statement, 101 FCL (Framework Class Library), fields (member variables), 134 access to (see access modifiers) compared to properties, 168 default values for, 142 initializers for, 144 initializing, 144 location of, 138 static, 151–153 with same name as parameters, 148 (see also properties) FIFO (first-in, first-out) collection (see queues) file extensions for data sources, 24 for dynamic link libraries, 23 for executable programs, 23 for MSIL, 10 for programs, for project metadata, 24 for solutions, 23 for source files, 22 File menu, 29 FileCopier application example, 411–414 Button events, handling, 426–431 source code for, 431–439 TreeView controls, populating, 415–422 TreeView events, handling, 422–426 files, inserting into source code, 32 Fill property, Rectangle element, 453 Finalize( ) method, Object, 238 finalizing objects, 154–155, 238 finally blocks, 368–370 Find and Replace options, Edit menu, 30 Find in Files, Edit menu, 31 Find Symbol, Edit menu, 31 Find( ) method, lists, 308 FindAll( ) method, lists, 308 first-in, first-out (FIFO) collection (see queues) float type (C#), 47, 49 folder for projects (see location of projects) Index | 655 www.it-ebooks.info for loops, 109–117 condition expression not specified in, 116 counter variable in, 110 counter variable not initialized in, 114 exiting, 111 iteration outside of header, 115 modulus operator controlling, 110 skipping to next iteration of, 112–114 foreach loops, 203, 303, 306 foreign key, 482 forever loops, 116 Format menu, 42 Format( ) method, strings, 334 formatting screen output, 52 forms, 404–411 forward slash (see slash) Fowler, Martin (Refactoring: Improving the Design of Existing Code), 41 Fraction class example, 243–248, 254–257 Framework Class Library (FCL), Friedl, Jeffrey E F (Mastering Regular Expressions, Third Edition), 30, 351 functions, member (see methods) G Gamma, Erich (Design Patterns), 386 garbage collection, 154 generalization, 220 generic collections, 292 foreach loop used with, 303, 306 interfaces for, 302–307 lists (List class), 307–319 queues, 319–322 stacks, 322–325 get accessors, 171, 297 GetEnumerator( ) method dictionaries, 326 lists, 308 queues, 320 stacks, 322 GetHashCode( ) method, Object, 238, 326 GetRange( ) method, lists, 308 GetType( ) method, Object, 238 Go To, Edit menu, 32 "goes to" operator (=>), 400 goto statement, 101, 105 greater than operator (>), 76 greater than or equal operator (>=), 76 656 | Index Grid element, XAML, 445, 448 accessing resources from, 468 properties for, 449 with stack panels, 461–466 H Harold, Elliotte Rusty (XML in a Nutshell), 444 has-a relationship between classes, 126 hash function, 238 heap, 138, 156–159, 199 Height property, Label element, 448 Hejlsberg, Anders (developer of C#), Hello World example, 6–9, 11–16 Hello WPF example, 443–447 Helm, Richard (Design Patterns), 386 Help menu, 44 HelpLink property, Exception class, 371 hiding base class methods, 223, 225 hit counts on breakpoints, 183 Hungarian notation, 413 Hurwitz, Dan Learning ASP.NET 3.5, 17, 432 Programming NET Windows Applications, 404 Programming ASP.NET, 43 I ICollection interface, 293 ICollection interface, 303 IComparable interface, 310 IComparable interface, 303 IComparer interface, 303, 314–319 IDE (integrated development environment), 16–17 identifier of class, 135 identifiers, 53 IDictionary interface, 303 IDictionary interface, 326 IDisposable interface, 154 IEnumerable interface, 293 IEnumerable interface, 303–307 IEnumerator interface, 303 if statement, 88–94 nested, 95–98 short-circuit evaluation of, 92–94 single-statement if blocks in, 90–92 if else statement, 94, 96 IL (Microsoft Intermediate Language), 10 www.it-ebooks.info IList interface, 303 Images class, 467 ImageURL class, 466 implementer of interface, 260, 261 implements relationship, 261 implicit conversions, 56, 253 implicit keyword, 254 implicitly typed variables, 55, 497 Import and Export Settings command, 43 increment by operator (++), 73–75 increment operators, 72–75, 80 incremental search, 33 indentation, changing, 33 index operator ([ ]), 198, 199 indexers for collections, 294 accessors for, 297 assignment to, 298 creating, 293–298 on types other than integers, 298–302 IndexOf( ) method arrays, 214 lists, 308 strings, 343, 344 inheritance, 127, 222–226 initialization, 53, 55 initializers field, 144 object, 146 inner joins, 484 input from console (see ReadLine( ) method) Insert File As Text, Edit menu, 32 Insert Snippet command, IntelliSense, 35, 36 Insert( ) method lists, 308 StringBuilder, 349 InsertRange( ) method, lists, 308 instance members, 148 instantiating objects, 135 int type (C#), 47, 48 Int16 type (.NET), 47 Int32 type (.NET), 47 Int64 type (.NET), 47 integrated development environment (IDE), 16–17 IntelliSense, 35 interfaces, 260–262 casting to, 270, 271 for collections, 293, 303 combining, 279 compared to abstract base classes, 261 defining, 265 determining whether an object implements, 271–276 explicit implementation of, 285–288 extending, 276–279 for generic collections, 302–307 implementer of, 260, 261 implementing, 262–266 implementing multiple, 267–269, 285–288 methods of, overriding, 280–285 naming conventions for, 261, 262 polymorphism used by, 262 searching for, 31 internal access modifier, 139 intrinsic types, 47 An Investigation into the Laws of Thought, on Which Are Founded the Mathematical Theories of Logic and Probabilities (Boole), 50 is operator, 271–276 is-a relationship, 127, 219 (see also specialization) IsMatch( ) method, Regex, 352 Item property dictionaries, 326 lists, 307 iteration (see loops) J jagged arrays, 210–213 JIT (Just In Time) compiler, 10 Johnson, Ralph (Design Patterns), 386 joins in LINQ queries, 502–505 in SQL queries, 484 jump statements (see unconditional branching) Just In Time (JIT) compiler, 10 K K&R style for braces, 93 Kernighan, Brian W (The C Programming Language), 93 keyboard shortcuts, 29 keys, 29 Keys property, dictionaries, 326 keywords, Index | 657 www.it-ebooks.info L Label control, 412 Label element, XAML, 445, 448 lambda expressions, 400, 499 lambda operator (=>), 400 Language Integrated Query (see LINQ) last-in, first-out (LIFO) collection (see stacks) LastIndexOf( ) method arrays, 214 lists, 308 strings, 344 Learning ASP.NET 3.5 (Liberty et al.), 17, 432 left angle bracket (

Ngày đăng: 16/03/2014, 00:20

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • About This Book

    • Who This Book Is For

    • How This Book Is Organized

    • Conventions Used in This Book

    • Support: A Note from Jesse Liberty

    • Using Code Examples

    • We’d Like to Hear from You

    • Safari® Books Online

    • Acknowledgments

      • Jesse Liberty

      • Brian MacDonald

      • C# and .NET Programming

        • Installing C# Express

        • C# 3.0 and .NET 3.5

        • The .NET Platform

        • The .NET Framework

        • The C# Language

        • Your First Program: Hello World

        • The Compiler

        • Examining Your First Program

        • The Integrated Development Environment

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

Tài liệu liên quan