by C Petzold — It is certainly not a tutorial for learning the language, but a great book to read after you’ve become adept at C# programming.
30 KB – 267 Pages
PAGE – 2 ============
.NET Book Zero Charles Petzold Version 1.1 Page 1 Table of Contents C hapter 1. Why This Book? .. .. 2 Chapter 2. Why .NET? .. .. .. 5 Chapter 3. Runtimes and SDKs .. .. 7 Chapter 4. Edit, Compile, Run, Disassemble .. . 11 Chapter 5. Strings and the Console .. 22 Chapter 6. Primitive Data T ypes .. .. 51 Chapter 7. Operators and Expressions .. . 68 Chapter 8. Selection and Iteration .. .. 75 Chapter 9. The Stack and the Heap .. 83 Chapter 10. Arrays .. .. .. 88 Chapter 11. Methods and Fields .. .. 95 Chapter 12. Exception Handling .. . 108 Chapter 13. Classes, Structures, and Objects .. .. 117 Chapter 14. Instance Methods .. . 126 Chapter 15. Constructors .. .. 135 Chapter 16. Concepts of Equality .. .. 144 Chapter 17. Fields and Properties .. .. 153 Chapter 18. Inheritance .. .. .. 166 Chapter 19. Virtuali ty .. .. .. 175 Chapter 20. Operator Overloading .. . 191 Chapter 21. Interfaces .. .. .. 202 Chapter 22. Interoperability .. . 206 Chapter 23. Dates and Times .. .. 210 Chapter 24. Events and Delegates .. . 221 Chapter 25. Files and Streams .. 226 Chapter 26. String Theory .. . 250 Chapter 27. Generic s .. .. 253 Chapter 28. Nullable Types .. .. 259
PAGE – 3 ============
.NET Book Zero Charles Petzold Version 1.1 Page 2 Chapter 1 . Why This Book? with the stuff the r eader needs to know before read ing Chapter One . Chapter Zero might be a refresher course in subjects the reader once knew but has now for got – ten, or it might be a quick – and – dirty summary of prerequisites for the rest of the book. This boo k originated as a Chapter Zero in my book Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation (Micro soft Press, 2006) , which is about the new Windows client program – and Microsoft Windows Vi sta . I wanted Applications = Code + Markup to focus almost exclusively on the Windows Presentation Foundation. I knew t here was enough to cover with out going into the basics of general .NET programming and C#. Yet, I would know . I started writing a Chapter Zero for the book that would summarize all the basics of .NET and C# for the C and C++ programmers who might be coming to .NET for the very first time. It soon became evident that this Chapter Zero would be very long . It occurred to me that I could extract the material and make it a book on its own. And so I did and this is it . What you have in your hands (or are reading on a screen) is an introduction to C# and those topics in .NET that are typically found in all .NE T programming. C# is a modern type – safe and object – oriented programming language based on the syntax of C and (to a certain extent) C++ and Java . Even if an experienced C++ programmer, you might be in for a few sur – prises. You may think you know the difference between a class and a struct , for example, but the difference between a class and a struct in C# is completely different from C++. (Th at difference is actually one of the lamest features of C++ and one of the most profound features of C#.) For that reason and others, I approach object – oriented programming con – cepts in this book very first time. However, I do expect you to have some programming experience with a C – learnin g C# as a first programming language, you might be better off with a slower, gentler introduction, such as my book Programming in the Key of C# : A Primer for Aspiring Programmers (Microsoft Press, 2003).
PAGE – 4 ============
.NET Book Zero Charles Petzold Version 1.1 Page 3 The contents of .NET Book Zero are copyrighted by me , but the book is freely distributable. You can give it to whomever you want. You can make – er work without my permission. reference this book on your own Web site, I ask that you do so using a link to the page on my Web site where th is book is found. That page is: http://www.charlespetzold.c om/dotnet of the book and download the source code I show in the pages that follow. written. Come to my web site, www.charlespetzold.com and check them out. These other books cost you money . B ut you will be pleased to know that I receive a small percentage of the price you pay for each book. Th at money help s me pay my rent and feed my – self, and enables me to write more books in the future. In writing this book, I dr awn upon some of my earlier writing about C# and the .NET Framework. Some of the earlier chapters are revisions of Chapter 1 in Programming Mi crosoft Windows with C# (Microsoft Press, 2001), which is an introduction to Windows Forms programming. Some of the later chapters were drawn from appendices of that book. The chap – ters specific to the object – oriented programming aspects of C# were drawn f rom my book Programming in the Key of C# . y a couple books on that subject. But there are two essential books that every C# and .NET programmer should have . The first essential book is The C# Programming Language by Anders Hejlsberg, Scott Wiltamuth, and Peter Golde ( 2 nd edition, Addison – Wesley, 2006). This book is the official technical specification of the C# language. It is certainly not a tutorial for le arning the language, but a great book to read after Downloadable versions of T h e C # Programming Language are available under the title C# Language Specification from this Web page: http://msdn2.microsoft.com/en – us/vcsharp/aa336809.aspx Because the online title C# Language Specification is actually more accurate than the book title The C# Programming Language , I will refer to the online title rather than the book title when I sometimes refer to the book using chapter and section numbers.
PAGE – 5 ============
.NET Book Zero Charles Petzold Version 1.1 Page 4 CLR via C# (Microsoft Press, 2006), which is actually the second edition of Applied Microsoft .NET Framework Pr ogramming . There are many subtle and interesting aspects of .NET programming that book explores in much more dep th the pages ahead that . In .NET Book Zero and my other books, I tend to focus more on the C# languag e and the .NET Framework class libraries rather than Microsoft Visual Studio . As you probably know, Visual Studio is the primary programming environment for creating .NET applications . You might want to supplement your C# and .NET studies with a book speci fic to Visual Studio. Because this book is intended to teach C# and the rudiments of .NET, much of the code I show in the pages ahead target s the traditional (and largely obsolete) command line using character – mode programming interfaces. I am well aware t graphical environments, and that you might consider learning about character – mode programming to be a complete waste of your time. This is not so. The character – formatting techniques you learn here are directly applicable to graphical programming as well . This book is written in tutorial style , which means that it is intended to be read sequentially. The reader is encouraged to type in the programs as they are encountered in the book, to run them, and experiment with them. * * * V ersion 1.0 of this book was posted to w ww.charlespetzold.com/dotnet on December 4 , 2006. Version 1.1 was posted on January 1 , 200 7 . It incorporated many minor corrections reported by Larry Danielle, Paul Doughert y, Paul Duggan, David Evans, Th orsten Franz, Konstantin Korobkov , Tyson Maxwell, Ryan McFarren , and Larry Smith .
PAGE – 6 ============
.NET Book Zero Charles Petzold Version 1.1 Page 5 Chapter 2 . Why .NET? The Microsoft .NET term .NET) is a collection of software technologies that began emerging from Microsoft Corporation a round the turn of the century. The first version of .NET was release d early in 2002, and version 1.1 came out in 2003. Version 2.0 was released late in 2005, and Version 3.0 followed in late 2006 . A good overview of the .NET releases can be found in the Wiki – pedia entry on the Microsoft .NET Framework: http://en.wikipedia.org/wiki/.NET_Framework From the end – fa irly invisible. Th e savvier us er might feel enlightened to know that .NET is basically a collection of dynamic link libraries . These DLLs might already be installed along with Windows XP on a new machine, or they might be installed during the process of installing an applic ation that uses .NET. The late st version of Windows Microsoft Windows Vista includes the .NET Framework 3.0 as an intrinsic part of its architecture. contains everything you need to writ e Web applications or client applica – tions apps . If you are a programmer, and you write (or want to write) Web applica – tions or Windows client applications – ing .NET, then reading this book is a good move . Congratulations on getting started ! You can program for .NET in a variety of programming languages. How – ever, any language you use for .NET programming must meet a set of minimum requirements to order to use the .NET class libraries. These requirements are known as the .NET Common Language Specification or CLS. Related to the CLS is the .NET Common Type System (CTS) which defines the basic data types (such as integer, floating point, and string) that .NET languag es support. The CLS and CTS are in turn part of the Common Language Infrastructure (CLI). The CLI is an ISO standard and an ECMA standard. When you compile one of your .NET programs , the program is generally compiled to a processor – independent intermediate language that resem – bles machine code. This intermediate language was once called Micro – soft Intermediate Language (MSIL), just called IL . B ut the most proper term is now t he Common Intermediate Lan guage (CIL).
PAGE – 8 ============
.NET Book Zero Charles Petzold Version 1.1 Page 7 Chapter 3 . Runtimes and SDKs To run .NET pr have some soft – ware installed that is variously The term means that a software developer like yo urself can distri – installation for an application th a t requires the .NET Framework to run. To also need to install the .NET Framework Software Development Kit (SDK). Both the runtime and the SDK are free and both are generally downloadable from the same or related Web pages. To determine what versions of .NET (if any) are currently installed on your machine, th e following Knowledge Base article can help: http://support.microsoft.com/kb/318785 For installations of the .NET Framework 1.1 and the SDK , go to this page : http://msdn2.microsoft.com/netframework/aa569264.aspx Although this page includes a redistributable for .NET 1.1, it is recom – mended that end users install the .NET 1.1 runtime components as part of a Windows update. For the .N ET Framework 2.0, go here: http://msdn2.microsoft.com/netframework/aa731542.aspx For the .NET Framework 3.0, go here: http://msdn2.microsoft.com/windowsvista/aa904955.aspx The SDK is referred to A s of this writing, .NET version 3.0 is fairly recent, but it is likely to become the rosoft Windows Vista. However, you may want to target a lesser version of .NET if you The most recent version of Microsoft Visual Studio is Visual Studio 2005, which incorporates the .NET Framework 2.0 SDK . The next version of Visual Studio will incorporate the .NET Framework 3.0 SDK. Meanwhile, if you want to do .NET 3.0 programming with Visual Studio to install the 3.0 SDK along with Visual Studio 2005 . If you need to pro – gram for a specific subsystem of .NET 3.0 (such as the Windows Presen –
PAGE – 9 ============
.NET Book Zero Charles Petzold Version 1.1 Page 8 tation Foundation or the Windows Communication Foundation or the Windows Workflow Foundation) you can install extensions to Visual Studio 2005. These are available as links from the .NET Framework 3.0 page. Microsoft also makes available a free Visual C# 2005 Express Edition that you can download here: http://msdn.microsoft.com/vstudio/express/visualcsharp This package installs the .NET 2.0 runtime and a good chunk of the SDK. (You can install the 2.0 SDK in addition to the Visual C# Express Edition .) The installation asks if you want to install MSDN, which stands for Microsoft Developer Network and refers to documentation that in – cludes documentation. program for .NET. The .NET Framework SDK comes with a command – line version of the C# compiler, and you c an use that. However, Visual Studio and Visual C# simplify several aspects of .NET programming. Besides the compiler itself, perhaps the most important part of the SDK is the documentation of the .NET class libraries. When you install one of the SDKs, the SDK itself appears on the Windows start menu, and a Doc only installed Visual C# 2005 Express Edition, you can bring up the documentation by selecting Contents from the Help menu of Visual C# . ) The .NET documentation is displayed by the Document Explorer applica – tion. O n the left side of the D ocument Explorer window is a pane that you can switch between Content and Index views with a tab at the bot – tom. The pane on the right side shows information on the selected item. Select the Content tab. I want you to find the documentation of the .NET Class Library heading in the following hierarchy: .NET Framework SDK Reference Class L ibrary With a later SDK, the hierarchy is a bit shorter: .NET Framework SDK Class Library Or:
PAGE – 10 ============
.NET Book Zero Charles Petzold Version 1.1 Page 9 .NET Framework Development Class Library it by the large list of entries. Many of the early entries begin with the word Microsoft . The later entries begin with the word System . is the basic class documentation of the .NET Framework time with it . You can also access the .NET Framework documentation online at this page: http://msdn2.microsoft.com/library/aa388745.aspx The top – level entries in this long list that begin with the words Microsoft or System are known as namespaces . The namespaces serve to separate the .NE T Framework into functional groups. For example, System.Win – dows. Forms is the basic namespace for Windows Forms. Namespaces also help avoid problems resulting from duplicate class names. The .NET Framework can have different classes with the same names. I f these classes are in different three classes named Timer , for example, all in di fferent namespaces. Some of these namespaces will become an intimate part of your life; As the popular tee – shirt says, The most important namespace is System be referring to most in this book. A few other namespaces are often useful, even in traditional character – mode programs. The System. – Globalization namespace contains classes that help you tailor your programs to an international market. The System.Collections and System. Collections.Generic contain classes that help you store inform – ation in familiar collections s uch as queues, stacks, and dictionaries. The System.IO namespace contains essential classes for working with files and streams, and System.Xml supplements those classes for working with XML. see a number of types defined in the namespace . Most of the se types a re classes. Some are structures. Others are interfaces, enumerations, and delegates. in the pages ahead. Open up a class or structure, and you see members of that type . These members can include constructors, fields, methods, proper ties, and events reading this book or any other .N want to have th e .NET documentation open and ready for browsing.
PAGE – 11 ============
.NET Book Zero Charles Petzold Version 1.1 Page 10 To quickly find a particular item in the class documentation, click the Index tab in the left pane . In the Look For field , enter for: Timer classes with their namespaces in parentheses. Select the one you want, and the first page of the class documentation will appea r. You can then click the Sync With Table Of Contents button on the toolbar to get back to the Contents view and continue exploring the particular class. (In the .NET Framework 1.1 SDK, it works a little differently. There is no separate pane for index res ults; the three Timer classes are listed separately in the index.) Besides providing all the class documentation of the .NET Framework, another important role of the .NET Framework documentation is the teaching of humility. You will never, ever, come close to any type of familiarity with the entire .NET class library. (But you can always try.)
30 KB – 267 Pages