A Programmer's Introduction to C# - APress.pdf

(1071 KB) Pobierz
Microsoft Word - A Programmer's Introduction to C# - APress.doc
Release Team[oR] 2001
[x] Programming
94472133.005.png
A Programmer's Introduction to C#
by Eric Gunnerson
ISBN: 1893115860
Companion Web Site
This book takes the C programmer through the all the
details—from basic to advanced-- of the new Microsoft C#
language.
Table of Contents
Colleague Comments
Back Cover
Synopsis
Written as an introduction to the new C#, this guide takes the experienced C
programmer a few steps beyond the basics. It covers objects, data types, and
flow control, and even delves into some background on the new Microsoft
NET Frameworks environment. Keeping in mind that this is for those familiar
with C (and even Java), the book goes into some of the advanced features
and improvements found in this new language. It also offers a comparison
between C#, C++, Visual Basic, and Java.
A Programmer's Introduction to C# ....................................................................................... - 9 -
Foreword .................................................................................................................................... - 10 -
About This Book ....................................................................................................................... - 10 -
Introduction .............................................................................................................................. - 11 -
Why Another Language? .......................................................................................................... - 11 -
C# Design Goals ....................................................................................................................... - 11 -
The C# Compiler and Other Resources .................................................................................... - 12 -
Chapter 1: Object-Oriented Basics .......................................................................................... - 13 -
Overview ................................................................................................................................... - 13 -
What Is an Object? .................................................................................................................... - 13 -
Inheritance ................................................................................................................................. - 13 -
Polymorphism and Virtual Functions ....................................................................................... - 14 -
Encapsulation and Visibility ..................................................................................................... - 16 -
Chapter 2: The .Net Runtime Environment ............................................................................ - 16 -
Overview ................................................................................................................................... - 16 -
The Execution Environment ..................................................................................................... - 17 -
Metadata .................................................................................................................................... - 18 -
Assemblies ................................................................................................................................ - 19 -
Language Interop ...................................................................................................................... - 19 -
Attributes ................................................................................................................................... - 19 -
Chapter 3: C# Quickstart ........................................................................................................... - 20 -
Overview ................................................................................................................................... - 20 -
Hello, Universe ......................................................................................................................... - 20 -
Namespaces and Using ............................................................................................................. - 20 -
Namespaces and Assemblies .................................................................................................... - 21 -
Basic Data Types ...................................................................................................................... - 22 -
Classes, Structs, and Interfaces ................................................................................................. - 23 -
- 2 -
Apress © 2000, 358 pages
94472133.006.png 94472133.007.png 94472133.008.png 94472133.001.png
Statements ................................................................................................................................. - 23 -
Enums ....................................................................................................................................... - 23 -
Delegates and Events ................................................................................................................ - 24 -
Properties and Indexers ............................................................................................................. - 24 -
Attributes ................................................................................................................................... - 24 -
Chapter 4: Exception Handling ................................................................................................ - 25 -
Overview ................................................................................................................................... - 25 -
What's Wrong with Return Codes? ........................................................................................... - 25 -
Trying and Catching .................................................................................................................. - 25 -
The Exception Hierarchy .......................................................................................................... - 26 -
Passing Exceptions on to the Caller .......................................................................................... - 28 -
User-Defined Exception Classes ............................................................................................... - 30 -
Finally ....................................................................................................................................... - 31 -
Efficiency and Overhead ........................................................................................................... - 33 -
Design Guidelines ..................................................................................................................... - 33 -
Chapter 5: Classes 101 ............................................................................................................. - 33 -
Overview ................................................................................................................................... - 33 -
A Simple Class .......................................................................................................................... - 33 -
Member Functions .................................................................................................................... - 35 -
ref and out Parameters ............................................................................................................... - 36 -
Overloading ............................................................................................................................... - 38 -
Chapter 6: Base Classes And Inheritance ............................................................................. - 39 -
Overview ................................................................................................................................... - 39 -
The Engineer Class ................................................................................................................... - 39 -
Simple Inheritance .................................................................................................................... - 40 -
Arrays of Engineers .................................................................................................................. - 42 -
Virtual Functions ....................................................................................................................... - 45 -
Abstract Classes ........................................................................................................................ - 47 -
Sealed Classes ........................................................................................................................... - 50 -
Chapter 7: Class Member Accessibility .................................................................................. - 51 -
Overview ................................................................................................................................... - 51 -
Class Accessibility .................................................................................................................... - 51 -
Using internal on Members ....................................................................................................... - 51 -
The Interaction of Class and Member Accessibility ................................................................. - 52 -
Chapter 8: Other Class Stuff .................................................................................................... - 52 -
Overview ................................................................................................................................... - 53 -
Nested Classes ........................................................................................................................... - 53 -
Other Nesting ............................................................................................................................ - 53 -
Creation, Initialization, Destruction .......................................................................................... - 54 -
Overloading and Name Hiding ................................................................................................. - 56 -
Static Fields ............................................................................................................................... - 57 -
Static Member Functions .......................................................................................................... - 58 -
Static Constructors .................................................................................................................... - 59 -
Constants ................................................................................................................................... - 59 -
readonly Fields .......................................................................................................................... - 60 -
Private Constructors .................................................................................................................. - 63 -
Variable-Length Parameter Lists .............................................................................................. - 63 -
Chapter 9: Structs (Value Types) ............................................................................................ - 65 -
Overview ................................................................................................................................... - 65 -
A Point Struct ............................................................................................................................ - 65 -
Boxing and Unboxing ............................................................................................................... - 66 -
Structs and Constructors ........................................................................................................... - 66 -
- 3 -
94472133.002.png
Design Guidelines ..................................................................................................................... - 67 -
Chapter 10: Interfaces ............................................................................................................... - 67 -
Overview ................................................................................................................................... - 67 -
A Simple Example .................................................................................................................... - 67 -
Working with Interfaces ............................................................................................................ - 68 -
The as Operator ....................................................................................................................... - 70 -
Interfaces and Inheritance ......................................................................................................... - 71 -
Design Guidelines ..................................................................................................................... - 72 -
Multiple Implementation ........................................................................................................... - 72 -
Interfaces Based on Interfaces .................................................................................................. - 77 -
Chapter 11: Versioning Using new and override ................................................................... - 77 -
Overview ................................................................................................................................... - 77 -
A Versioning Example .............................................................................................................. - 77 -
Chapter 12: Statements and Flow of Execution .................................................................... - 79 -
Overview ................................................................................................................................... - 79 -
Selection Statements ................................................................................................................. - 79 -
Iteration Statements ................................................................................................................... - 81 -
Jump Statements ....................................................................................................................... - 85 -
Definite Assignment ................................................................................................................. - 85 -
Chapter 13: Local Variable Scoping ........................................................................................ - 88 -
Overview ................................................................................................................................... - 88 -
Chapter 14: Operators ............................................................................................................... - 89 -
Overview ................................................................................................................................... - 89 -
Operator Precedence ................................................................................................................. - 89 -
Built-In Operators ..................................................................................................................... - 90 -
User-Defined Operators ............................................................................................................ - 90 -
Numeric Promotions ................................................................................................................. - 90 -
Arithmetic Operators ................................................................................................................. - 90 -
Relational and Logical Operators ............................................................................................. - 92 -
Assignment Operators ............................................................................................................... - 94 -
Type Operators .......................................................................................................................... - 94 -
Chapter 15: Conversions .......................................................................................................... - 96 -
Overview ................................................................................................................................... - 96 -
Numeric Types .......................................................................................................................... - 96 -
Conversions of Classes (Reference Types) ............................................................................. - 100 -
Conversions of Structs (Value Types) .................................................................................... - 103 -
Chapter 16: Arrays ................................................................................................................... - 103 -
Overview ................................................................................................................................. - 103 -
Array Initialization .................................................................................................................. - 103 -
Multidimensional and Jagged Arrays ..................................................................................... - 104 -
Arrays of Reference Types ..................................................................................................... - 105 -
Array Conversions .................................................................................................................. - 106 -
System.Array Type ................................................................................................................. - 106 -
Chapter 17: Strings .................................................................................................................. - 107 -
Overview ................................................................................................................................. - 107 -
Operations ............................................................................................................................... - 107 -
Converting Objects to Strings ................................................................................................. - 109 -
Regular Expressions ................................................................................................................ - 111 -
Chapter 18: Properties ............................................................................................................ - 115 -
Overview ................................................................................................................................. - 115 -
Accessors ................................................................................................................................ - 115 -
Properties and Inheritance ....................................................................................................... - 116 -
- 4 -
94472133.003.png
Use of Properties ..................................................................................................................... - 116 -
Side Effects When Setting Values .......................................................................................... - 117 -
Static Properties ...................................................................................................................... - 119 -
Property Efficiency ................................................................................................................. - 120 -
Chapter 19: Indexers ............................................................................................................... - 120 -
Overview ................................................................................................................................. - 121 -
Indexing with an Integer Index ............................................................................................... - 121 -
Indexers and foreach .......................................................................................................... - 125 -
Design Guidelines ................................................................................................................... - 128 -
Chapter 20: Enumerators ........................................................................................................ - 128 -
Overview ................................................................................................................................. - 128 -
A Line Style Enumeration ....................................................................................................... - 128 -
Enumerator Base Types .......................................................................................................... - 130 -
Initialization ............................................................................................................................ - 130 -
Bit Flag Enums ........................................................................................................................ - 131 -
Conversions ............................................................................................................................. - 131 -
Chapter 21: Attributes .............................................................................................................. - 132 -
Overview ................................................................................................................................. - 132 -
Using Attributes ...................................................................................................................... - 133 -
An Attribute of Your Own ...................................................................................................... - 136 -
Reflecting on Attributes .......................................................................................................... - 138 -
Chapter 22: Delegates ............................................................................................................. - 139 -
Overview ................................................................................................................................. - 140 -
Using Delegates ...................................................................................................................... - 140 -
Delegates as Static Members .................................................................................................. - 141 -
Delegates as Static Properties ................................................................................................. - 143 -
Chapter 23: Events .................................................................................................................. - 145 -
Overview ................................................................................................................................. - 145 -
A New Email Event ................................................................................................................ - 145 -
The Event Field ....................................................................................................................... - 147 -
Multicast Events ...................................................................................................................... - 147 -
Sparse Events .......................................................................................................................... - 147 -
Chapter 24: User-Defined Conversions ................................................................................ - 149 -
Overview ................................................................................................................................. - 149 -
A Simple Example .................................................................................................................. - 149 -
Pre- and Post- Conversions ..................................................................................................... - 151 -
Conversions Between Structs .................................................................................................. - 152 -
Classes and Pre- and Post- Conversions ................................................................................. - 157 -
Design Guidelines ................................................................................................................... - 163 -
How It Works .......................................................................................................................... - 165 -
Chapter 25: Operator Overloading ........................................................................................ - 167 -
Overview ................................................................................................................................. - 167 -
Unary Operators ...................................................................................................................... - 167 -
Binary Operators ..................................................................................................................... - 167 -
An Example ............................................................................................................................. - 168 -
Restrictions .............................................................................................................................. - 169 -
Design Guidelines ................................................................................................................... - 169 -
Chapter 26: Other Language Details .................................................................................... - 169 -
Overview ................................................................................................................................. - 170 -
The Main Function .................................................................................................................. - 170 -
Preprocessing .......................................................................................................................... - 171 -
Preprocessing Directives ......................................................................................................... - 171 -
- 5 -
94472133.004.png
Zgłoś jeśli naruszono regulamin