Introduction

C# 2010 All-in-One For Dummies represents a different way of looking at programming languages. Rather than present the standard For Dummies format, which includes only 350 pages on quite a large subject, the book was expanded to include a broader scope and just a few pages were added.

So, although you find all the original C# For Dummies goodness in this book, you also find discussions about Visual Studio, Windows Presentation Foundation (WPF), service-oriented development, Web development, and a host of other topics. This book is a one-stop shop for a C# developer.

The C# programming language is a powerful and, at some nine years old, relatively mature descendant of the earlier C, C++, and Java languages. Programming with C# is lots of fun, as you're about to find out in this book.

Microsoft created C# as a major part of its .NET initiative. The company turned over the specifications for the C# language to the ECMA (pronounced "ek-ma") international standards committee in the summer of 2000 so that any company can, in theory, come up with its own version of C# written to run on any operating system, on any machine larger than a calculator.

When the first edition of this book was published, the Microsoft C# compiler was the only game in town, and its Visual Studio .NET suite of tools was the only way to program C# (other than at the Windows command line). Since then, however, Visual Studio has undergone three major revisions — the latest is Visual Studio 2010. And, at least two other players have entered the C# game.

You can now write and compile C# programs on Windows and a variety of Unix-based machines using implementations of .NET and C#, such as Mono (www.mono-project.com), an open source software project sponsored by Novell Corporation. Version 1.2 was released in November 2006. Though Mono lags Microsoft .NET by half a version or so, it appears to be moving fast, having implemented basically all of .NET 1.1 and much of .NET 2.0, along with those versions of C#.

Both Mono and a less well developed competitor, Portable .NET (www.dotgnu.org/pnet.htm), claim to run C# programs on Windows and a variety of Unix flavors, including Linux and the Apple Macintosh operating system. At the time of this writing, Portable .NET reaches the greater number of flavors, whereas 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 online booksellers.)

Note

Open source software is written by collaborating groups of volunteer programmers and is usually free to the world.

A description of how to make 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 Visual Studio has the most mature versions of C# and .NET and the most feature-filled toolset for programming with them.

Note: Though three authors contributed to this book, saying I rather than we throughout the main text seemed more economical, so that's what we (or I) do throughout.

What's New in C# 4.0

Although much of C# 4.0 is still virtually the same as the previous version, this new version adds some exciting new features, most of which revolve around COM Interop, to assist with Office development. The big new additions that this book covers include these topics:

  • Dynamic types: Functional programming is all the rage these days, with the cool kids programming in Ruby and Haskell. Functional programming certainly has some benefits that have a place in the more tightly woven world of C#, and dynamic typing is one of them. As supported in C++ and Visual Basic, dynamic types allow runtime declaration when you don't know the type of a variable and then have the compiler figure it out. Properly used, dynamic typing is quite powerful; poorly used, it's quite dangerous.

  • Named and optional parameters: In C# 3.0, you had to provide a value for every parameter in a method call. In C# 4.0 — again, to optimize interactions with COM — you can mark parameters as optional and accept outside objects that have optional parameters.

  • Variance in generics: Although objects in previous versions of C# are variant, generic collections of objects are invariant. This statement means that although the compiler accepts an apple when you're asked for a fruit, it doesn't accept a basket of apples when you're asked for a basket of fruit. This issue is fixed in C# 4.0.

Leaving aside a few of the more esoteric and advanced additions, we mention a few smaller items here and there as appropriate. (Don't worry if parts of this introduction are Geek to you. You'll get there.)

Because the features of C# 4.0 all tie together for use in a single major operation — COM Interop — we bundled discussions of these features in the back of this book, in Book VIII. Throughout this book, we have updated chapters from the original C# 3.0 material wherever it seemed appropriate.

About This Book

The goal of this book is to explain C# to you. To write usable programs, you need a specific coding environment. We're betting that most readers will use Microsoft Visual Studio, although we suggest alternatives. Because this book is an All-in-One, we give you comprehensive coverage of Visual Studio in Book IV.

The original version of C# For Dummies (like all programming language books) focused on C# as a language, not all of the things you do with C#. This version of the book — in the mondo-size All-in-One format — covers Windows development, Web development, service development, and .NET Framework development, such as graphics and databases.

Our goal is to make a one-stop shop for development with Microsoft products, though there is indeed more to the topics than fits in this book. Office development isn't covered, for instance. SharePoint has way too much going on to cover it along with everything else (although Bill co-authored VSTO For Dummies). This book is designed to handle the vast majority of C# users, though 20 percent of our readers will be working on something that we don't cover. Sorry about that — we'll try to stretch the book to 1,000 pages next time.

Another point is that every programming problem is different. Although many different situations are covered between the covers of this book, your specific situation is different. Some interpolation has to be taking place. If you have a question about how your personal situation fits in, send the author an e-mail at csharpfordummies.net and we'll try to help.

What You Need in Order to Use This Book

You need, at minimum, the .NET Common Language Runtime (CLR) before you can even execute the programs generated by C#. Visual Studio 2010 copies the CLR onto your machine as part of its installation procedure. Alternatively, you can download the entire .NET package, including the C# compiler and many other useful tools, from the Microsoft Web site at msdn.microsoft.com. Look for the .NET Software Development Kit (SDK). The book's Web site at csharpfordummies.net explains how to get these items.

Tip

If all you need is C#, you can download the free version of Visual Studio, Visual C# 2010 Express, from msdn.microsoft.com/vstudio/express. The Express versions include the new C# 4.0 features. Alternatively, see SharpDevelop (www.icsharpcode.net), a good, free Visual Studio "workalike," which are provided on the Web site for this book.

Tip

You can still create most of the programs in this book using earlier versions of Visual Studio, such as Visual Studio 2008, if you need to. The exceptions are the programs that cover the new features available only in C# 4.0, which we describe in Book VIII.

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? Though this book is divided into eight minibooks, we use an even easier subdivision.

Books I and II comprise the bulk of the original C# For Dummies book, and they cover the C# language, updated for version 4.0. Books III and IV cover technologies that are peripheral to C#. Books V, VI, and VII cover the three main types of development you do in C# — Windows Presentation Foundation, Web development, and service-oriented programming. We finish with the (thankfully short) Book VIII, about new C# 4.0 features.

If you're brand new, start at the beginning and read the first two minibooks. You'll discover a lot. Really. It will seem as though you're reading a lot of text, but it is engaging and has interesting examples.

If you're using the .NET Framework (which you probably are), read Book III as well. If you're using Visual Studio, read Book IV. (Note that we use Visual Studio 2010 Professional edition, so if you're using Express or Ultimate, your screens might look slightly different.)

Finally, you can focus on your project type — Books V, VI, and VII are specific to project type, and you can pick and choose what to read. These three minibooks are organized more as a collection of related articles than as discrete book units. You'll find them easier to use that way.

How This Book Is Organized

Here's a brief rundown of what you'll find in each part of this book.

Book I: The Basics of C# Programming

This minibook is the first of two that are based on the original C# For Dummies.

Book II: Object-Oriented C# Programming

In Book II, we dig into the meat of the matter and discuss which tasks C# is good for. This minibook covers how to create good class libraries and use the built-in libraries correctly. We also give you a good dose of theory and practical knowledge.

Book III: Designing for C#

.NET is essentially the set of libraries that you get to use with C#. This broad topic covers almost everything that Microsoft products can do, from coding for Windows Mobile to accessing XML files. Book III covers four of the most-needed topics:

  • Databases

  • Files

  • Graphics

  • Security

Book IV: A Tour of Visual Studio

Because Visual Studio is the tool that 95 percent of C# programmers use, it's the tool that is the focus of Book IV. It covers the use, optimization, and customization of this graphical user interface.

Book V: Windows Development with WPF

This minibook is an unconventional choice for Windows development. For years in .NET — since its inception — the choice for Windows developers was Windows Forms, the successor to the Ruby engine in Visual Basic 6. That's just how you build Windows applications.

Even with the introduction of C# 4.0 and Visual Studio 2010, Windows Forms is likely the most common choice for development of Windows applications. However, it's not the future. Windows Presentation Foundation — built on the XML derivative XAML — is the future.

For that reason, we include chapters on Windows development with WPF. If you're looking for Windows Forms 101, we include it at csharpfordummies.net.

Book VI: Web Development with ASP.NET

Some people would argue that ASP.NET Web Forms is falling by the wayside because of ASP.NET MVC, but we believe that it's a version or two away. In this book, we look at ASP.NET application creation and form controls and the other usual suspects. (Look for MVC in the next edition of this book, though.)

Book VII: Service-Oriented Development

On the topic of Web services, we decided to give you an overview. Our experience shows that ASP.NET Web Services, Windows Communication Foundation (WCF), and REpresentational State Transfer, or ReST, are all used in the wild, so we describe a piece of all of them.

Book VIII: New Features in C# 4.0

In the last minibook in this book, we describe some of the new features in C# 4.0, with a focus on the COM Interop changes.

Icons Used in This Book

Throughout the pages of this book, we use the following icons to highlight important information:

Note

This scary-sounding icon flags technical information that you can skip on your first pass through the book.

Tip

The Tip icon highlights a point that can save you a lot of time and effort.

Note

Remember this information. It's important.

Warning

Try to retain any Warning information you come across, too. This one can sneak up on you when you least expect it and generate one of those extremely hard-to-find bugs. Or, it may lead you down the garden path to La-La Land.

Note

This icon identifies code samples you can find on the book's Web sites. Csharp102.info has the samples for Book I and II, and many of the articles. Csharpfordummies.net has a current blog, errata, and examples for the rest of the book. This feature is designed to save you some typing time when your fingers start to cramp, but don't abuse it: You gain a better understanding of C# when you enter the programs yourself and then use them as test beds for your explorations and experiments in C#.

Conventions Used in This Book

Throughout this book, we use several conventions to help you get your bearings. Terms that aren't "real words," such as the names of program variables, appear in this font to minimize confusion. Program listings are offset from the text this way:

use System;
namespace MyNameSpace
{
  public class MyClass
  {
  }
}

Each listing is followed by a clever, insightful explanation. Complete programs are included on the Web site for your viewing pleasure; small code segments are not.

When you see a command arrow, as in the instruction "Choose File

Conventions Used in This Book

About this book's Web site

Two main Web sites expand on the content in this book.

  • At csharp102.info, you can find support for the original C# For Dummies book as well as a host of bonus material. A set of utilities is also included. We've used the SharpDevelop utility enough to know that it can handle the task of writing almost any program example in this book (with the possible exception, for now, of the new LINQ features). The Reflector tool lets you peek under the covers to see what the compiler has created from your lovely C# source code. The NUnit testing tool, wildly popular among C# programmers, makes testing your code easy, whether it's in Visual Studio or SharpDevelop.

  • At csharpfordummies.net, you can find the source code for all projects in this book, updated for Visual Studio 2010. We give you a set of links to other resources and a (short, we hope) list of any errata found in this book. You can also contact the authors at this site.

Additionally, you can find access to both sites at this book's companion Web site — check out www.dummies.com/go/csharp2010aiofd.

If you encounter a situation that you can't figure out, check the Frequently Asked Questions (FAQ) list at the original Web site for the C# For Dummies book, at csharp102.info.

In addition, both sites include bonus chapters, a list of any mistakes that may have crept into the book, and other material on C# and programming that you may find useful. Finally, you can find links to the authors' e-mail addresses, in case you can't find the answer to your question on the site.

Where to Go from Here

Obviously, your first step is to figure out the C# language — ideally, by using C# 2010 All-in-One For Dummies, of course. You may want to give yourself a few months of practice in writing simple C# programs before taking the next step of discovering how to create graphical 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: 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 Bill's house, garbage collection is quite deterministic: It's every Wednesday morning.) Here's a description of a few large C# sites:

  • msdn.microsoft.com/vcsharp, the C# home page, directs you to all sorts of C# and .NET resources.

  • blogs.msdn.com/csharpfaq is a C# blog with Frequently Asked Questions.

  • msdn.microsoft.com/vcsharp/team/blogs is composed of the personal blogs of C# team members.

  • www.c-sharpcorner.com and www.codeproject.com are two major C# sites that have articles, blogs, code, job information, and other C#-related resources.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.174.111