Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Similar documents
Microsoft. Microsoft Visual C# Step by Step. John Sharp

Microsoft Visual C# Step by Step. John Sharp

C# Programming: From Problem Analysis to Program Design. Fourth Edition

COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

CHAPTER 1: INTRODUCING C# 3

Object-Oriented Programming in C# (VS 2015)

Object-Oriented Programming in C# (VS 2012)

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CHAPTER 1: INTRODUCTION TO THE IDE 3

OVERVIEW ENVIRONMENT PROGRAM STRUCTURE BASIC SYNTAX DATA TYPES TYPE CONVERSION

Introduction to Programming Using Java (98-388)

Visual C# 2008 How to Program, 3/E Outline

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Introduce C# as Object Oriented programming language. Explain, tokens,

Course Syllabus C # Course Title. Who should attend? Course Description

"Charting the Course... MOC Programming in C# with Microsoft Visual Studio Course Summary

Programming C# 5.0. Ian Griffiths O'REILLY' Beijing Cambridge * Farnham Kbln Sebastopol Tokyo

Visual Basic 2008 How to Program, 4/E Outline

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Course Hours

Oracle 10g: Java Programming

Contents. Before You Begin. Object Technology: A Brief Review

Prerequisites: The student should have programming experience in a high-level language. ITCourseware, LLC Page 1. Object-Oriented Programming in C#

Murach s Beginning Java with Eclipse

5/23/2015. Core Java Syllabus. VikRam ShaRma

Variable Scope The Main() Function Struct Functions Overloading Functions Using Delegates Chapter 7: Debugging and Error Handling Debugging in Visual

OracleAS 10g R3: Java Programming

DOT NET Syllabus (6 Months)

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CO Java SE 8: Fundamentals

DOT NET SYLLABUS FOR 6 MONTHS

Deitel Series Page How To Program Series

Absolute C++ Walter Savitch

Before You Begin. and Visual Basic 1

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

Practical C++ Programming

Standard. Number of Correlations

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

"Charting the Course... MOC A Introduction to Web Development with Microsoft Visual Studio Course Summary

Level: M.Ed. Credit Hour: 3 (2+1) Semester: Third Teaching Hour: 80(32+48)

Programming in Visual Basic with Microsoft Visual Studio 2010

Contents. 1 Introduction to Computers, the Internet. Before You Begin

Introduction to C++/CLI 3. What C++/CLI can do for you 6 The rationale behind the new syntax Hello World in C++/CLI 13

C# is intended to be a simple, modern, general-purpose, objectoriented programming language. Its development team is led by Anders Hejlsberg.

Microsoft Visual Studio 2010

Contents. Illustrations. 1 Introduction to Computers, the Internet, the Web and C# 1

Beginning ASP.NET. 4.5 in C# Matthew MacDonald

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

E Oracle 10g Java Bootcamp I v1

1 OBJECT-ORIENTED PROGRAMMING 1

Chapters are PDF documents posted online at the book s Companion Website (located at

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Windows Presentation Foundation Programming Using C#

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

DIPLOMA IN PROGRAMMING WITH DOT NET TECHNOLOGIES

C# Programming in the.net Framework

Java 1.8 Programming

Contents. Preface. Introduction. Introduction to C Programming

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Java Programming with Eclipse

JAVA CONCEPTS Early Objects

"Charting the Course... Java Programming Language. Course Summary

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Learning to Program in Visual Basic 2005 Table of Contents

Table of Contents. Preface... xxi

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

Table of Contents Preface Bare Necessities... 17

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

[CHAPTER] 1 INTRODUCTION 1

This page intentionally left blank

Beginning Perl. Third Edition. Apress. JAMES LEE with SIMON COZENS

Contents. Using Interpreters... 5 Using Compilers... 5 Program Development Life Cycle... 6

C# Syllabus. MS.NET Framework Introduction

JavaScript & DHTML Cookbool(

Contents Chapter 1 Introduction to Programming and the Java Language

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Objects First with Java

Building Java Programs

Introduction to Programming Microsoft.NET Applications with Visual Studio 2008 (C#)

T.Y.B.Sc. Syllabus Under Autonomy Mathematics Applied Component(Paper-I)

Problem Solving with C++

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Whom Is This Book For?... xxiv How Is This Book Organized?... xxiv Additional Resources... xxvi

Programming in C# with Microsoft Visual Studio 2010

DC69 C# &.NET DEC 2015

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

WA1278 Introduction to Java Using Eclipse

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

SECURED PROGRAMMING IN.NET DETAILED TRAINING CONTENT INDUSTRIAL TRAINING PROGRAM ( )

"Charting the Course... MOC A Developing Data Access Solutions with Microsoft Visual Studio Course Summary

Migrate Your Skills to Microsoft.NET Framework 2.0 and 3.0 using Visual Studio 2005 (C#)

The C# Programming Language. Overview

C# 2008 and.net Programming for Electronic Engineers - Elektor - ISBN

Computer Programming C++ (wg) CCOs

"Charting the Course to Your Success!" MOC B Programming in C# Course Summary

Programming in C# for Experienced Programmers

Transcription:

Learning C# 3.0 Jesse Liberty and Brian MacDonald O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Table of Contents Preface xv 1. C# and.net Programming 1 Installing C# Express 2 C# 3.0 and.net 3.5 3 The.NET Platform 4 The.NET Framework 4 The C# Language 5 Your First Program: Hello World 6 The Compiler 10 Examining Your First Program 11 The Integrated Development Environment 16 Summary 17 Test Your Knowledge: Quiz 18 Test Your Knowledge: Exercise 19 2. Visual Studio 2008 and C# Express 2008 20 Before You Read Further 22 The Start Page 22 Projects and Solutions 22 Project Types 24 Templates 25 Inside the Integrated Development Environment 26 Building and Running Applications 28

Menus 29 The File Menu 29 The Edit Menu 30 The View Menu 36 The Refactor Menu 41 The Project Menu 41 The Build Menu 41 The Debug Menu 41 The Data Menu 41 The Format Menu 42 The Tools Menu 42 The Window Menu 43 The Help Menu 44 Summary 44 Test Your Knowledge: Quiz 45 Test Your Knowledge: Exercises 45 3. C# Language Fundamentals 46 Statements 46 Types 47 Numeric Types 48 Nonnumeric Types: char and bool 49 Types and Compiler Errors 50 WriteLine() and Output 51 Variables and Assignment 52 Definite Assignment 54 Implicitly Typed Variables 55 Casting 56 Constants 58 Literal Constants 58 Symbolic Constants 58 Enumerations 60 S trings 63 Whitespace 63 Summary 64 Test Your Knowledge: Quiz 65 Test Your Knowledge: Exercises 66 vi I Table of Contents

Takle nf Cnntentgvii 4. Operators 68 Expressions 68 The Assignment Operator (=) 69 Mathematical Operators 69 Simple Arithmetic Operators (+, *, /) 70 The Modulus Operator (%) 71 Increment and Decrement Operators 72 The Calculate and Reassign Operators 72 Increment or Decrement by 1 73 The Prefix and Postfix Operators 73 Relational Operators 75 Logical Operators and Conditionals 77 The Conditional Operator 78 Operator Precedence 79 Summary 81 Test Your Knowledge: Quiz 82 Test Your Knowledge: Exercises 83 5. Branching 85 Unconditional Branching Statements 86 Conditional Branching Statements 88 if Statements 88 Single-Statement if Blocks 90 Short-Circuit Evaluation 92 if else Statements 94 Nested if Statements 95 switch Statements 98 Fall-Through and Jump-to Cases 101 Switch an string Statements 102 ReadLine( ) and Input 103 Iteration (Looping) Statements 104 Creating Loops with goto 105 The while Loop 106 The do...while Loop 108 The for Loop 109 Summary 118 Test Your Knowledge: Quiz 119 Test Your Knowledge: Exercises 120

viii I Table of Contents 6. Object-Oriented Programming 121 Creating Models 123 Classes and Objects 123 Defining a Class 124 Class Relationships 125 The Three Pillars of Object-Oriented Programming 126 Encapsulation 126 Specialization 127 Polymorphism 128 Object-Oriented Analysis and Design 129 Summary 130 Test Your Knowledge: Quiz 131 Test Your Knowledge: Exercises 131 7. Classes and Objects 133 Defining Classes 134 Instantiating Objects 135 Creating a Box Class 137 Access Modifiers 138 Method Arguments 139 Return Types 141 Constructors 142 Initializers 144 Object Initializers 146 Anonymous Types 146 The this Keyword 147 Static and Instance Members 148 Invoking Static Methods 149 Using Static Fields 151 Finalizing Objects 154 Memory Allocation: The Stack Versus the Heap 155 Summary 161 Test Your Knowledge: Quiz 162 Test Your Knowledge: Exercises 163 8. Inside Methods 165 Overloading Methods 165

Encapsulating Data with Properties 168 The get Accessor 171 The set Accessor 172 Automatic Properties 173 Returning Multiple Values 173 Passing Value Types by Reference 175 out Parameters and Definite Assignment 177 Summary 178 Test Your Knowledge: Quiz 178 Test Your Knowledge: Exercises 179 9. Basic Debugging 180 Setting a Breakpoint 181 Using the Debug Menu to Set Your Breakpoint 183 Setting Conditions and Hit Counts 183 Examining Values: The Autos and Locals Windows 184 Setting Your Watch 188 The Call Stack 189 Stopping Debugging 190 Summary 191 Test Your Knowledge: Quiz 192 Test Your Knowledge: Exercises 193 10. Arrays 197 Using Arrays 197 Declaring Arrays 198 Understanding Default Values 199 Accessing Array Elements 199 Arrays and Loops 200 The foreach Statement 203 Initializing Array Elements 204 The params Keyword 204 Multidimensional Arrays 205 Rectangular Arrays 206 Jagged Arrays 210 Array Methods 213 Sorting Arrays 214 Table of Contents 1 ix

x Table of Contents Summary 216 Test Your Knowledge: Quiz 217 Test Your Knowledge: Exercises 218 11. Inheritance and Polymorphism 219 Specialization and Generalization 219 Inheritance 222 Implementing Inheritance 222 Calling the Base Class Constructor 225 Hiding the Base Class Method 225 Controlling Access 226 Polymorphism 227 Creating Polymorphic Types 227 Overriding Virtual Methods 230 Using Objects Polymorphically 230 Versioning with new and override 232 Abstract Classes 234 Sealed Classes 237 The Root of All Classes: Object 237 Summary 240 Test Your Knowlege: Quiz 241 Test Your Knowledge: Exercises 241 12. Operator Overloading 243 Designing the Fraction Class 243 Using the operator Keyword 244 Creating Useful Operators 248 The Equals Operator 248 Conversion Operators 253 Summary 257 Test Your Knowledge: Quiz 258 Test Your Knowledge: Exercises 259 13. Interfaces 260 What Interfaces Are 260 Implementing an Interface 262 Defining the Interface 265 Implementing the Interface an the Client 266 Implementing More Than One Interface 267

Casting to an Interface 270 The is and as Operators 270 Extending Interfaces 276 Combining Interfaces 279 Overriding Interface Methods 280 Explicit Interface Implementation 285 Summary 288 Test Your Knowledge: Quiz 290 Test Your Knowledge: Exercises 290 14. Generics and Collections 292 Generics 292 Collection Interfaces 293 Creating Your Own Collections 293 Creating Indexers 293 Indexers and Assignment 298 Indexing an Other Values 298 Generic Collection Interfaces 302 The IEnumerable<T> Interface 303 Framework Generic Collections 307 Generic Lists: List<T> 307 Generic Queues 319 Generic Stacks 322 Dictionaries 325 Summary 328 Test Your Knowledge: Quiz 329 Test Your Knowledge: Exercises 330 15. Strings 331 Creating Strings 332 String Literals 332 Escape Characters 332 Verbatim Strings 333 The ToString( ) Method 333 Manipulating Strings 334 Comparing Strings 334 Concatenating Strings 336 Copying Strings 337 Table of Contents 1 xi

xii I Tobte of Contents Testing for Equality 339 Other Useful String Methods 341 Finding Substrings 344 Splitting Strings 346 The StringBuilder Class 348 Regular Expressions 350 The Regex Class 351 Summary 353 Test Your Knowledge: Quiz 354 Test Your Knowledge: Exercises 355 16. Throwing and Catching Exceptions 357 Bugs, Errors, and Exceptions 358 Throwing Exceptions 358 Searching for an Exception Handler 358 The throw Statement 359 The try and catch Statements 361 How the Call Stack Works 364 Creating Dedicated catch Statements 366 The finally Statement 368 Exception Class Methods and Properties 370 Custom Exceptions 374 Summary 377 Test Your Knowledge: Quiz 378 Test Your Knowledge: Exercises 378 17. Delegates and Events 380 Delegates 381 Events 385 Publishing and Subscribing 386 Events and Delegates 387 Solving Delegate Problems with Events 394 The event Keyword 395 Using Anonymous Methods 399 Lambda Expressions 400 Summary 401 Test Your Knowledge: Quiz 402 Test Your Knowledge: Exercises 403

18. Creating Windows Applications 404 Creating a Simple Windows Form 404 Using the Visual Studio Designer 405 Creating a Real-World Application 411 Creating the Basic UI Form 412 Populating the TreeView Controls 415 Handling the TreeView Events 422 Handling the Button Events 426 Source Code 431 Summary 439 Test Your Knowledge: Quiz 440 Test Your Knowledge: Exercises 441 19. Windows Presentation Foundation 442 Your First WPF Application 443 WPF Differences from Windows Forms 447 Using Resources 450 Animations 452 Triggers and Storyboards 453 Animations As Resources 456 C# and WPF 460 Grids and Stack Panels 461 Adding Data 466 Using the Data in the XAML 468 Defining the ListBox 468 Event Handling 470 The Complete XAML File 471 Summary 474 Test Your Knowledge: Quiz 476 Test Your Knowledge: Exercises 476 20. ADO.NET and Relational Databases 477 Relational Databases and SQL 478 Installing the Northwind Database 478 Tables, Records, and Columns 481 Normalization 482 Declarative Referential Integrity 482 SQL 483 Table of Contents 1 xiii

The ADO.NET Object Model 485 DataTables and DataColumns 485 DataRelations 485 Rows 485 DataAdapter 486 DbCommand and DbConnection 486 DataReader 486 Getting Started with ADO.NET 486 Summary 489 Test Your Knowledge: Quiz 490 Test Your Knowledge: Exercises 491 21. LINQ 492 Querying In-Memory Data 492 Anonymous Types and Implicitly Typed Variables 497 Lambda Expressions 499 Ordering and Joining 500 Using LINQ with SQL 505 Using the Object Relational Designer 508 Summary 513 Test Your Knowledge: Quiz 514 Test Your Knowledge: Exercises 515 Appendix: Answers to Quizzes and Exercises 517 Index 649 xiv Table of Contents