Κυριακή 30 Δεκεμβρίου 2007
absolute: Every expression has a value and a type. In a declaration such as
int n, you can easily see that the variable n is an int. Further, you can reasonably
assume that the type of a calculation n + 1 is an int. However, what
type is the constant 1?
The type of a constant depends on two things: its value and the presence of an
optional descriptor letter at the end of the constant. Any integer type less than
2 billion is assumed to be an int. Numbers larger than 2 billion are assumed to
be long. Any floating pointing number is assumed to be a double.
Table 3-4 demonstrates constants that have been declared to be of a particular
type. The case of these descriptors is not important. Thus, 1U and 1u are
equivalent.
Τετάρτη 12 Δεκεμβρίου 2007
Throughout this book, we use several conventions to help you out. Terms that are not “real words,” such as the name of some program variable, appearin this font to minimize the confusion factor. Program listings are offset from text as follows:
use System;
namespace MyNameSpace
{
public class MyClass
{
}
}
Each listing is followed by a clever, insightful explanation. Complete programs are included on the CD-ROM for your viewing pleasure. Small code segments are not. Finally, you’ll see command arrows, as in the phrase, “Choose File➪Open-With➪Notepad.” That means choose the File menu option. Then, from the pull-down menu that appears, choose Open With. Finally, from the resulting submenu, choose Notepad. Where to Go from Here Obviously, the first step is to figure out the C# language, ideally using C# 2005 For Dummies, of course. You may want to give yourself a few months of writing simple C# programs before taking on the next step of discovering how to create Windows applications. Give yourself many months of Windows application experience before you branch out into writing programs intended to be distributed over the Internet. In the meantime, you can keep up with C# goings and comings in several locations. First, check out the official source: http://msdn.microsoft.com/msdn. In addition, various programmer Web sites have extensive material on C#, including lively discussions all the way from how to save a source file to the relative merits of deterministic versus nondeterministic garbage collection. Around my house, garbage collection is very deterministic: It’s every Wednesday morning. Here are a few large C# sites:
www.gotdotnet.com, the .NET team’s official site
http://msdn.microsoft.com, which gets you to related team sites, including C# and the .NET Framework
Introduction 7
http://blogs.msdn.com/csharpfaq, a C# Frequently Asked Questions
blog
http://msdn.microsoft.com/vcsharp/team/blogs, which is comprised
of personal blogs of C# team members
www.cs2themax.com
One of the authors maintains a Web site, www.chucksphar.com, containing a set of Frequently Asked Questions (FAQs). If you encounter something that you can’t figure out, try going there — maybe the FAQs have already answered your question. In addition, the site includes a list of any mistakes that may have crept into the book. Finally, and we do mean finally, you can find a link to the authors’ e-mail addresses, in case you can’t find the answer to your question on the site.
Τρίτη 11 Δεκεμβρίου 2007
Throughout the pages of this book, we use the following icons to highlight important information.
This icon flags technical stuff that you can skip on the first reading.
The Tip icon highlights a point that can save you a lot of time and effort.
Remember this. It’s important.
Remember this, too. This one can sneak up on you when you least expect it and generate one of those really hard-to-find bugs.This icon identifies code that you can find on the CD-ROM that comes with this book. This feature is designed to save you some typing when your fingers start to cramp, but don’t abuse it. You’ll gain a better understanding of C# by entering the programs yourself.
The C# programming language is a powerful, relatively new descendant ofthe earlier C, C++, and Java languages. Programming with it is a lot of fun,as you’re about to find out in this book.
Microsoft created C# as a major part of its .NET initiative. For what are probably political reasons, Microsoft turned the specifications for the C# language over to the ECMA (pronounced ek-ma) international standards committee in the summer of 2000, long before .NET was a reality. In theory, any company can come up with its own version of C# written to run on any operating system, on any machine larger than a calulator.
When the first edition of this book came out, Microsoft’s C# compiler was the only game in town, and its Visual Studio .NET suite of tools offered the only way to program C# (other than at the Windows command line). Since then, however, Visual Studio has gone through two major revisions — Visual Studio 2003 and, very recently, Visual Studio 2005. And at least two other players have entered the C# game.
It’s now possible to write and compile C# programs on a variety of Unix-based machines using either the Mono or Portable .NET implementations of .NET and C#:
Mono (www.go-mono.com) is an open-source software project sponsored by Novell Corporation. Version 1.1.8 came out in June 2005. While Mono lags Microsoft’s .NET, just now implementing the 1.1 version that Microsoft released a couple of years ago, it appears to be moving fast.
Portable .NET, under the banner of Southern Storm Software and DotGNU (www.dotgnu.org/pnet.html), is also open-source. Portable .NET is at version 0.7.0 as of this writing.
Both Mono and Portable .NET claim to run C# programs on Windows and a variety of Unix flavors, including Linux and Apple’s Macintosh operating system. At this writing, Portable .NET reaches the greater number of flavors, while Mono boasts a more complete .NET implementation. So choosing between them can be complicated, depending on your project, your platform, and your goals. (Books about programming for these platforms are becoming available already. Check www.amazon.com.)
Open-source software is written by collaborating groups of volunteer programmers and is usually free to the world.
Making C# and other .NET languages portable to other operating systems is far beyond the scope of this book. But you can expect that within a few years, the C# Windows programs you discover how to write in this book will run on all sorts of hardware under all sorts of operating systems — matching the claim of Sun Microsystems’ Java language to run on any machine. That’s undoubtedly a good thing, even for Microsoft. The road to that point is still under construction, so it’s no doubt riddled with potholes and obstacles to true universal portability for C#. But it’s no longer just Microsoft’s road. For the moment, however, Microsoft’s Visual Studio has the most mature versions of C# and .NET and the most feature-filled tool set for programming with them.
If all you need is C#, I’ve included a bonus chapter called “C# on the Cheap” on the CD that accompanies this book. That chapter tells you how you can write C# code virtually for free. (You’ll be missing lots of amenities, including the nice visual design tools that Visual Studio 2005 provides, but you can write Windows code without them, especially the kind of code in this book. Bonus Chapter 5 explains how.)
Note: Two authors wrote this book, but it seemed more economical to say “I” instead of “we,” so that’s what we (I?) do throughout.
What’s New in C# 2.0
While C# version 2.0 does have a number of small changes here and there, most of C# 2.0 is still virtually the same as the previous version. The big new additions that this book covers include the following: Iterator blocks: An iterator is an object that lets you step through all the items in a collection of objects. That’s always been possible, but C# 2.0 makes it far simpler to implement. Bonus Chapter 3 on the CD helps you take advantage of the simplicity and flexibility of iterator blocks. Chapter 15 covers collections.
Generics: This is the big one! Generic features allow you to write highly general, more flexible code. It’s a powerhouse — a programmer’s dream. Chapter 15 shows you how to write far simpler and more type-safe code using generics. Leaving aside a few of the more esoteric and advanced additions, we’ll mention a few smaller items here and there as appropriate. (Don’t worry if parts of this Introduction are Greek to you. You’ll get there.)
2 C# 2005 For Dummies About This Book
The goal of this book is to explain C# to you, but to write actual programs you need a specific coding environment. We’re betting that most readers will be using Microsoft Visual Studio, although we do provide alternatives. In basing the book on Visual Studio, we’ve tried to keep the Visual Studio portions to a reasonable minimum. we could just tell you, “Run your program any way you want,” but instead we may say, “Execute your C# program from Visual Studio by pressing F5.” We want you to be able to focus on the C# language and not on the mechanics of getting simple things to work.
We realize that many, if not most, readers will want to use C# to write graphical Windows applications. C# is a powerful tool for programming graphical Windows applications, but that’s only one area for using C#, and this book must focus on C# as a language. We touch briefly on graphical Windows programs in Chapter 1, but you should get a good grasp of C# before seeking another source to understand Windows programming in full. We also realize that some power users will be using C# to build Web-ready, distributed applications; however, publishing limitations require us to draw the line somewhere. C# 2005 For Dummies does not tackle the challenges of distributed programming. The book does explain quite a bit of .NET, though, for the simple reason that much of C#’s power comes from the .NET Framework class libraries that it uses.
What You Need to Use the Book
At a minimum, you need the Common Language Runtime (CLR) before you can even execute the programs generated by C#. Visual Studio 2005 copies the CLR onto your machine for you as part of the installation procedure. Alternatively, you can download the entire .NET package, including the C# compiler and many other nice tools, from Microsoft’s Web site at http:// msdn.microsoft.com. Look for the .NET Software Development Toolkit (SDK). Bonus Chapter 5 on the CD explains how to get these items. You can still create most of the programs in this book with Visual Studio 2003, if you need to. The exceptions are those that cover the new features available only with C# 2.0, primarily generics and iterator blocks. A less costly C# Express 2005 version of Visual Studio 2005 is also available, and don’t overlook the cheap options covered in Bonus Chapter 5 on the CD.
Introduction 3
How to Use This Book
We’ve made this book as easy to use as possible. Figuring out a new language is hard enough. Why make it any more complicated than it needs to be? The book is divided into six parts. Part I introduces you to C# programming with Visual Studio. This part guides you step by step in the creation of two different types of programs. We strongly encourage you to start here and read these two chapters in order before branching out into the other parts of the book. Even if you’ve programmed before, the basic program framework created in Part I is reused throughout the book. The chapters in Parts II through V stand alone. We have written these chapters so that you can open the book to any one of them and start reading. If you’re new to programming, however, you will have to read Part II before you can jump ahead. But when you return to refresh your memory on some particular topic, you should have no trouble flipping to a section without the need to restart 20 pages back.
Of course, the Part of Tens finishes out the lineup, and there’s more on the CD that accompanies the book.
How This Book Is Organized
Here’s a brief rundown on what you’ll find in each part of the book.
Part I: Creating Your First C# Programs
This part shows you, step by step, how to write the smallest graphical Windows application possible using the Visual Studio 2005 interface. Part I also shows you how to create the basic nongraphical C# framework that’s used in the other parts of this book.
Part II: Basic C# Programming
At the most basic level, Shakespeare’s plays are just a series of words all strung together. By the same token, 90 percent of any C# program you ever write consists of creating variables, performing arithmetic operations, and controlling the execution path through a program. This part concentrates on these core operations.
4 C# 2005 For Dummies
Part III: Object-Based Programming
It’s one thing to declare variables here or there and to add them and subtract them. It’s quite another thing to write real programs for real people. Part III focuses on how to organize your data to make it easier to use in creating a program.
Part IV: Object-Oriented Programming
You can organize the parts of an airplane all you want, but until you make it do something, it’s nothing more than a collection of parts. It’s not until you fire up the engines and start the wings flapping that it’s going anywhere. In like fashion, Part IV explains how to turn a collection of data into a real object — an object that has internal members, sure, but an object that can mimic the properties of a real-world item. This part presents the essence of object-oriented programming.
Part V: Beyond Basic Classes
After the airplane gets off the ground, it has to go somewhere. Figuring out classes and the fundamentals of object-oriented programming is only a start. Part V takes the next step, introducing structures, interfaces, and generics, your gateway to more advanced object-oriented concepts — and the wild blue yonder.
Part VI: The Part of Tens
C# is great at finding errors in your programs — at times, it seems a little too good at pointing out my shortcomings. However, believe it or not, C# is trying to do you a favor. Every problem it finds is another problem that you would otherwise have to find on your own. Unfortunately, the error messages can be confusing. One chapter in this part presents the ten most common C# build error messages, what they mean, and how the heck to get rid of them. Many readers are coming to C# from another programming language. The second chapter in The Part of Tens describes the ten major differences between C# and its progenitor, C++.
Introduction 5
About the CD-ROM
the enclosed CD-ROM contains a host of goodies. First, you find all thesource code from this book. A set of utilities is also included. We don’t recommend the SharpDevelop utility for full-scale development of commercial programs, but it’s useful for writing small applications or making a quick change without waiting for Visual Studio to boot up. It’s perfectly adequate for everything in this book. The TextPad editor is Notepad on steroids. It makes a fine cheap platform for programming C#. The NUnit testing tool, wildly popular among C# programmers, makes testing your code easy, whether from Visual Studio, SharpDevelop, or TextPad. Finally, the CD contains a bunch of bonus chapters covering features and techniques that wouldn’t fit into the book. Don’t ignore the CD. Don’t forget the ReadMe file, which has all the most up-to-date information.