Programming C# Work1
Total Page:16
File Type:pdf, Size:1020Kb
Programming C# Jesse Liberty Publisher: O'Reilly First Edition July 2001 ISBN: 0-596-00117-7, 680 pages The goal of C# is to serve as a high-performance language for .NET development—one that is simple, safe, object-oriented, and Internet-centric. Programming C# teaches this new language in a way that experienced programmers will appreciate—by grounding its application firmly in the context of Microsoft's .NET platform and the development of desktop and Internet applications. 2 Programming C# Preface ................................................................................................................................11 About This Book .........................................................................................................11 How the Book Is Organized ...................................................................................11 Who This Book Is For ...............................................................................................13 C# Versus Visual Basic .NET .................................................................................13 C# Versus Java...........................................................................................................14 C# versus C++...........................................................................................................14 Conventions Used in This Book ............................................................................14 Support ..........................................................................................................................15 We'd Like to Hear from You...................................................................................15 Acknowledgements ...................................................................................................16 Part I: The C# Language ..................................................................................................17 Chapter 1. C# and the .NET Framework .......................................................................17 1.1 The .NET Platform..............................................................................................17 1.2 The .NET Framework ........................................................................................17 1.3 Compilation and the MSIL ..............................................................................19 1.4 The C# Language...............................................................................................19 Chapter 2. Getting Started:"Hello World".......................................................................21 2.1 Classes, Objects, and Types..........................................................................21 2.2 Developing "Hello World"................................................................................26 Just In Time Compilation ..................................................................................................29 2.3 Using the Visual Studio .NET Debugger....................................................29 Chapter 3. C# Language Fundamentals........................................................................33 3.1 Types ......................................................................................................................33 The Stack and the Heap ...................................................................................................34 3.2 Variables and Constants..................................................................................36 WriteLine( )..........................................................................................................................36 3.3 Expressions ..........................................................................................................42 3.4 Whitespace ...........................................................................................................42 3.5 Statements ...........................................................................................................43 Statement Blocks...............................................................................................................46 All Operators Are Not Created Equal..............................................................................47 Whitespace and Braces....................................................................................................53 3.6 Operators ..............................................................................................................56 Short-Circuit Evaluation ....................................................................................................61 3.7 Namespaces.........................................................................................................63 3.8 Preprocessor Directives...................................................................................65 Chapter 4. Classes and Objects......................................................................................69 4.1 Defining Classes .................................................................................................69 4.2 Creating Objects.................................................................................................73 4.3 Using Static Members.......................................................................................78 Static Methods to Access Static Fields ..........................................................................82 4.4 Destroying Objects............................................................................................82 How Finalize Works...........................................................................................................82 4.5 Passing Parameters...........................................................................................84 4.6 Overloading Methods and Constructors ....................................................89 3 4.7 Encapsulating Data with Properties............................................................91 4.8 Readonly Fields...................................................................................................94 Chapter 5. Inheritance and Polymorphism ....................................................................97 5.1 Specialization and Generalization................................................................97 About the Unified Modeling Language ...........................................................................97 5.2 Inheritance ...........................................................................................................99 5.3 Polymorphism....................................................................................................102 5.4 Abstract Classes ...............................................................................................107 5.5 The Root of all Classes: Object ..................................................................110 5.6 Boxing and Unboxing Types ........................................................................112 5.7 Nesting Classes.................................................................................................114 Chapter 6. Operator Overloading..................................................................................117 6.1 Using the operator Keyword........................................................................117 6.2 Supporting Other .NET Languages............................................................118 6.3 Creating Useful Operators ............................................................................118 6.4 Logical Pairs .......................................................................................................118 6.5 The Equals Operator .......................................................................................118 6.6 Conversion Operators.....................................................................................119 Chapter 7. Structs............................................................................................................125 7.1 Defining Structs................................................................................................125 7.2 Creating Structs................................................................................................127 Chapter 8. Interfaces.......................................................................................................131 Mix Ins...............................................................................................................................131 8.1 Implementing an Interface ..........................................................................131 8.2 Accessing Interface Methods.......................................................................141 8.3 Overriding Interface Implementations ....................................................147 8.4 Explicit Interface Implementation.............................................................150 Chapter 9. Arrays, Indexers, and Collections..............................................................159 9.1 Arrays ...................................................................................................................159 9.2 The foreach Statement ..................................................................................162 9.3 Indexers...............................................................................................................175 9.4 Collection Interfaces .......................................................................................182