Introduction

When it comes to programming, a little learning can indeed be a dangerous thing. If you read a book like C# 5.0 All-in-One for Dummies (Bill Sempf et al., 2013, For Dummies) or my book Stephens’ C# Programming with Visual Studio 2010 24-Hour Trainer (Rod Stephens, 2010, Wrox), after only a few weeks you can easily think you know everything there is to know about programming.

I clearly remember when I finished my first programming class. The language we used was UCSD Pascal, and after only one class, I knew it quite well. I knew how to use the language, how to draw simple graphics, and how to read and write files. I was quite sure that with enough work I could write just about any program imaginable.

Since then I’ve had plenty of opportunities to realize just how wrong I was. I’ve worked on projects in about a dozen different programming languages, each with its own strengths and idiosyncrasies. I’ve worked on elegantly architected systems where adding new features was a breeze, and I’ve worked on badly designed 50,000 plus line monstrosities where you might need to study the code for a week before changing a single line for fear of breaking everything else. Since then I’ve also studied complexity theory and learned that there are literally thousands of provably hard (NP-complete) programs that you cannot solve in a reasonable amount of time. (I talk about some of them in my book Essential Algorithms: A Practical Approach to Computer Algorithms, Rod Stephens, 2013, Wiley.)

Even by itself, C# is a complex and powerful programming language. It includes all the language features that you would expect in any high-level language such as structures and classes, methods, complex error handling (try, catch, and finally), branching statements (if-then and switch), several kinds of loops (for, foreach, and while), and several ways to break out of loops (break and return).

In addition to the complexities of the language itself, C# provides many auxiliary features that make it even more powerful and more complicated. Features let you execute query-like operations on arrays, use parallel processing, serialize and deserialize objects, and let a program inspect pieces of code to learn about the objects that it is using.

Finally, the environment that contains C# brings its own complexity. The .NET Framework contains more than 10,000 classes that give you access to libraries for cryptography, expression matching, interacting with the operating system, networking, and much more.

This book describes as much of that complexity as possible. It explains the pieces of the C# language in detail. It explains the syntax, data types, and control statements that go into C# applications. This book also describes some of the pieces of the .NET Framework that are most useful for building complex applications.

This book does not cover every possible topic related to C#, but it does cover the majority of the technologies that developers need to build sophisticated applications.

Who Should Read This Book

This book is intended for intermediate and advanced programmers who have already programmed in C# or some other language. This book describes C# in detail but it does so quickly and assumes you already understand basic programming concepts. If you’re a beginner, you can still use this book to learn to program in C#, but it will be a bit harder. If you get stuck, feel free to e-mail me at and I’ll try to put you back on the right track.

One of the main reasons this book assumes you know programming basics is it’s hard to find a simple order in which to present topics in depth. For example, declaring and using variables is one of the most basic concepts in programming. (This book covers that early in Chapter 4, “Data Types, Variables, and Constants.”) However, in C# variable declarations are different depending on whether they are inside a class’s method. (In some languages methods are also called procedures, subprocedures, routines, subroutines, or functions.) If you already know what a method is, then the book can cover variables in depth. If you are unfamiliar with methods, a book can present only the basics of variable declarations, then cover classes, and finally return to the topic of variables. This book assumes you know basics such as what a variable is and what methods are, so it can quickly move through topics without a lot of repeating and backtracking.

If fundamentals such as variable declarations, data types, classes, and arrays are familiar to you, you should have no problem with this book. The index and reference appendixes should be particularly useful in helping you remember the syntax for performing various C# tasks such as creating a class or making a generic method.

If you don’t know what data types are, what a for loop is, and what an if statement does, you Replace with “can probably pick those things up as you go along, but you may need to go back and reread a few chapters after you get the hang of things.”.

Approach

A program can interact with the user in many ways. It can read and write text in a console window. It can use Windows Forms and controls to provide a more graphical interface. A program can use Windows Presentation Foundation (WPF) controls to build an interface that is even more richly graphical and interactive than Windows Forms interfaces. Recently, a Windows Store program can use controls similar to those used in a WPF application to run in the Windows 8 operating system. Some programs provide no interface for the user and instead provide tools and services for other programs to use behind the scenes.

Building applications that use these different approaches takes a lot of work. The steps you take to build a WPF application are different from those you use to build a console application. However, no matter which kind of application you build, behind the user interface sits a bunch of good old C# code. You use the same syntax to create classes, loops, methods, and variables whether you’re building Windows Forms applications or WPF applications. The same C# language enables you to build applications to run in a console window, on the Windows desktop, in Windows 8, in a browser, or even in Windows Phone.

This book focuses on the C# programming language rather than on user interface design and construction. Chapter 2, “Writing a First Program,” explains how to start with different kinds of applications so that you can make applications to test the code, but the main focus is on the code behind the user interface.

This book also describes methods a program can use to interact with its environment. For example, the techniques and classes a program uses to create printouts or manipulate files aren’t actually part of the C# language, but they are essential for many applications.

Finally, this book describes some advanced subjects that are useful in many applications. These include such topics as using regular expressions to match patterns, parallel programming, serialization XML, databases, and cryptography.

Which Edition of Visual Studio Should You Use?

Visual Studio is the integrated development environment that is most often used to write C# programs. Because this book focuses on the C# language and not on user interface issues, you can use it with any edition of Visual Studio. You can use one of the free Express editions or one of the more complete Professional, Premium, or Ultimate editions.

The examples that go with this book, and that are available for download on this book’s website, were written in Visual Studio Express 2012 for Windows Desktop and were tested in Windows 8. I picked the Windows Desktop version because writing desktop applications is generally easier than writing Windows Store or Windows Phone applications. I picked the 2012 edition because a lot of people already have that version installed. Programs written in the 2012 edition should also be compatible with Visual Studio 2013 so you should be able to open them in the 2013 edition. If you have trouble opening the project, please let me know.

You can use other editions of Visual Studio to study C# programming. For example, you could use Visual Studio Express 2013 for Windows, which can build Windows Store applications. If you do, the C# code you write behind the scenes will be the same as the code you would write for Windows Desktop but building the user interface will be very different.

The C# language and the .NET Framework change from time to time but the basics remain the same. That means much of this book’s material applies to other versions of C# as well as different editions of Visual Studio. For example, there are some differences between C# 4.0 and C# 5.0, but the two versions are mostly the same. That means you can use this book even if you have an older version of Visual Studio installed, such as Visual Studio 2010, as long as you understand that a few things may not work. (The async and await keywords are the biggest differences.)

How This Book Is Organized

The chapters in this book are divided into five parts plus appendixes.

Part I: The C# Ecosystem

Chapters 1 through 3 explain how C# programs fit into the Visual Studio environment. They explain how C# code is converted into code that the computer can execute and how that conversion happens. They also explain what files go into a C# application and what those files contain.

Chapter 2 briefly explains how you can write simple console, Windows Forms, and WPF applications that can invoke C# code. The rest of the book focuses on that code and mostly ignores user interface issues.

Part II: C# Language Elements

Chapters 4 through 10 explain the bulk of the C# language and the objects that support it. They explain data types (string, float, and arrays), operators (+, *, and %), program control statements (if, while, and for), and error handling. They also explain how to edit and debug C# code.

Although Language-Integrated Query (LINQ) it is not strictly part of the C# language, it is closely tied to the language, so Chapter 8, “LINQ,” covers it. That chapter also covers Parallel LINQ (PLINQ), a parallel version of LINQ that can provide improved performance on multicore systems.

Part III: Object-Oriented Programming

Chapters 11 through 15 explain fundamental concepts in object-oriented programming (OOP) with C#. They explains how to define classes and inheritance hierarchies, use collection classes, and build generic classes and methods.

Part IV: Interacting with the Environment

Chapters 16 through 20 explain how an application can interact with its environment. They show how the program can create printouts, use configuration files, manipulate the filesystem, and download information from the Internet.

Part V: Advanced Topics

Chapters 21 through 27 cover more advanced topics that are useful in many advanced applications. They include such topics as recognizing patterns in text, parallel programming, using databases, serialization, reflection, and encrypting and decrypting data.

Part VI: Appendixes

Appendix A, “Solutions to Exercises,” provides outlines of solutions to the exercises described at the end of each chapter. Programs that implement many of the solutions are available for download on the book’s website. This appendix shows the most interesting parts of many of the programs, but to save space I omitted some of the less interesting details. Download the examples from www.wrox.com/go/csharp5programmersref to see all the code.

The book’s other appendixes provide a categorized reference of the C# language. You can use them to quickly review the syntax of a particular command or refresh your memory of what a particular class can do. The chapters earlier in the book give more context, explaining how to perform specific tasks and why one approach might be better than another. The appendixes provide a brief summary.

How to Use This Book

If you are an advanced C# programmer, you may want to skim the language basics covered in the first parts of the book. You still may find a few new details, so you might not want to skip these chapters entirely, but most of the basic language features are the same as in previous versions of C#.

Each chapter ends with a set of exercises you can use to test your understanding of the material covered in the chapter. Sometimes exercises point to more in-depth topics that don’t fit well in the chapter’s text. Even if you’re an advanced C# developer, you may want to read the exercises to make sure you didn’t miss anything.

Intermediate programmers and those with less C# experience should take these chapters a bit more slowly. The chapters in Part III, “Object-Oriented Programming,” cover particularly tricky topics. Learning all the variations on inheritance and interfaces can be rather confusing. If you are unfamiliar with these topics, plan to spend some extra time on those chapters.

Particularly if you have experience with some other programming language but not C#, you should spend some extra time on these first 10 or so chapters because they set the stage for the material that follows. It will be a lot easier for you to follow a discussion of file management or regular expressions if you are not confused by the error-handling code that the examples take for granted.

Programming is a skill best learned by doing. You can pick up the book and read through it quickly if you like (well, as quickly as you can, given how long it is), but the information is more likely to stick if you open Visual Studio and experiment with some programs of your own.

Throughout your work, you can refer to the appendixes to get information on specific classes, controls, and syntax. For example, you can turn to Appendix R, “Streams,” to quickly find classes you can use to manipulate files and directories. If you need more information, you can go back to Chapter 19, “File system Objects,” or check the online help. If you just need to refresh your memory of the basic classes and their methods, however, scanning Appendix R will be faster.

Necessary Equipment

To build C# programs, you need a copy of Visual Studio. To download different editions, go to www.visualstudio.com/downloads/download-visual-studio-vs.

To run Visual Studio, you need a reasonably modern, fast computer with a lot of memory. The exact requirements depend on the version and edition of Visual Studio you are using. To get the details, see the download page for the version you want to use. (I use a dual-core 64-bit 1.60 GHz Intel Core i5-4200U system with 8 GB of memory and 1 TB of hard disk space running Windows 8. It has a Windows Experience Index of 5.6 and handles Visual Studio with no problems.)

Much of C# 5 is compatible with earlier versions of C#, so if you’re using an older version of Visual Studio, you may be able to make most of this book’s examples work with your system. You cannot load the example programs directly into your version of Visual Studio, however. You need to open the source code files in an editor such as WordPad and copy and paste the significant portions of the code into your program.

Conventions

To help you get the most from the text and keep track of what’s happening, I used a number of conventions throughout the book.

For styles in the text:

  • Important words are italicized when they are introduced.
  • Keyboard strokes are shown like this: Ctrl+A.
  • Filenames, URLs, and code within the text are shown like this: persistence.properties.
  • Code is presented in the following two different ways:
    I use a monofont type for code examples.
    I use bolded type to emphasize code that's particularly important in the present context.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. Many of the examples in the text show only the code that is relevant to the current topic and may be missing some of the extra details that you need to make the example work properly.

All the source code used in this book is available for download at

www.wrox.com/go/csharp5programmersref

You can also search for the book at www.wrox.com to find the code. When at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book.

After you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of my books, like a spelling mistake or faulty piece of code, I would be grateful for your feedback. By sending in errata you may save another reader hours of frustration, and at the same time you will be helping me provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and to interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors and editors, other industry experts, and your fellow readers are present on these forums.

At p2p.wrox.com you can find a number of different forums that can help you not only as you read this book but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.
  2. Read the terms of use and click Agree.
  3. Complete the required information to join, as well as any optional information you want to provide, and click Submit.
  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

After you join, you can post new messages and respond to messages other users post. You can read messages at any time on the web. If you want to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

Using the P2P forums allows other readers to benefit from your questions and any answers they generate. I monitor my book’s forums and respond whenever I can help.

If you have other comments, suggestions, or questions that you don’t want to post to the forums, you can e-mail me at . I can’t promise to solve all your problems but I’ll try to help you if I can.

Important URLs

Here’s a summary of important URLs related to this book:

  • www.CSharpHelper.com—My C# website. Contains thousands of tips, tricks, and examples for C# developers.
  • p2p.wrox.com—Wrox P2P forums.
  • www.wrox.com—The Wrox website. Contains code downloads, errata, and other information. Search for the book by title or ISBN.
  • —My e-mail address. I hope to hear from you!
..................Content has been hidden....................

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