Introduction

So you want to learn C# programming? Excellent choice!

C# is a powerful, general-purpose programming language that lets you build desktop, Windows Store, Windows Phone, and web apps. C# provides all of the tools that you need to build a huge variety of applications such as:

  • Database applications
  • Point of sales systems
  • Two- and three-dimensional graphics programs
  • Image-processing and photo-manipulation systems
  • Computer-aided design (CAD) systems
  • Document layout and printing systems
  • Hardware control systems
  • High-performance games
  • Much, much more

Of course, you won't be able to solve every problem with C#. If you want a program that picks the winning number on a roulette wheel or that can predict stock prices, you may have better luck using tarot cards (or a degree in economics), but for tractable problems C# is a great choice.

This book is a self-paced guide to C# programming in the Visual Studio environment. It uses easy-to-follow lessons, reinforced by step-by-step instructions, screencasts, and supplemental exercises, to help you master C# programming quickly and painlessly. It explains how to write C# programs that interact with the user to read inputs, calculate results, and display outputs. It shows how to read and write files, make printouts, and use databases. It shows how to build programs that run on the Windows desktop, on tablet computers, and on Windows Phones.

This book won't make you an expert, but it will give you a solid understanding of how to write C# programs. When you've finished reading this book and working through the Try It sections and exercises, you'll be able to write non-trivial programs of your own. You may not be able to accurately pick winning lottery numbers (if you do, please let me know!), but you will be able to build some useful programs and you'll be ready to learn more about more specialized topics that interest you such as database programming, file processing, and graphics.

What's New in the Second Edition

This second edition has been modified and expanded to provide more material than the first edition, but it's not intended to be the second in a series. If you read the first edition, don't get the second edition because there's a lot of overlap.

The main differences between this edition and the first are:

  • More exercises (almost 400!)
  • More screencast videos (more than 12 hours!)
  • Windows Store apps
  • Windows Phone apps
  • A lot more material about Windows Presentation Foundation (WPF) and eXtensible Markup Language (XAML) (which you can use to build Windows Store and Windows Phone style apps)

To make room for the new material, some of the old material had to go. This edition doesn't cover:

  • The clipboard and drag-and-drop
  • Bitmap manipulation
  • Parallel programming
  • Console applications

I'd love to include those topics and many others, but there just isn't room in a book of this size.

Who This Book Is For

This book is for anyone who wants to learn how to write programs using C#. Whether you want to move into a lucrative career as a software developer, add a few new skills to your résumé, or pick up a fascinating new hobby, this book can get you started.

This book does not assume you have any previous programming experience. It assumes you're uninformed rather than an idiot or a dummy. It assumes you can turn your computer on and surf the web but that's about it for previous qualifications. It is suitable as a first programming book for high school or college students, but its self-paced hands-on approach also makes it ideal if you're trying to learn to program on your own.

(I don't want to receive a bunch of flaming e-mails complaining that the material in this book is too basic, so I'm warning you right now. If you've been programming in C++ or Visual Basic for 16 years, don't blame me if a lot of this material seems pretty simple to you. Instead of wasting your time complaining, go find a more advanced book.)

What This Book Covers (And What It Doesn't)

This book explains C# programming. It explains how to write, debug, and run applications that interact with the user and the computer. It shows how to understand object-oriented concepts, perform calculations, manipulate files and strings, produce printouts, and interact with simple databases. It explains how to run programs on your desktop, from the Windows Start menu, with a Windows tablet-style interface, and on a Windows Phone.

Programming in any language is an enormous topic, however, so this book doesn't cover everything. It doesn't explain how to design databases, build cryptographically secure web applications, create multithreaded programs that run on multiple CPUs, or build Xbox games, all tasks that are possible using C#. When you're finished reading this book, however, you'll be ready to move on to more advanced books that cover those topics.

The Wrox 24-Hour Trainer Approach

Educators have known for many years that different people use different learning styles most effectively. Different students may learn best by:

  • Reading a textbook
  • Looking at nonwritten material such as pictures and graphs
  • Listening to an instructor lecture
  • Watching someone demonstrate techniques
  • Doing exercises and examples

(Personally, I learn best by watching and doing.)

Good instructors try to incorporate material that helps students with all of these learning styles. Combining text, lecture, demonstration, discussion, and exercises lets every student pick up as much as possible using whichever methods work best.

Like a good instructor, this book uses materials that address each learning style. It uses text and figures to help visual learners, screencasts that provide visual demonstrations and auditory instruction, step-by-step instructions to help you do it yourself, and exercises for further study.

The book is divided into small, bite-sized lessons that begin with a discussion of a particular concept or technique, complete with figures, notes, tips, and other standard fare for instructional books. The lessons are short and tightly focused on a single task so you can finish each one in a single sitting. You shouldn't need to stop in the middle of a lesson and leave concepts half-learned (at least if you turn off your phone).

After describing the main concept, the lesson includes a Try It section that invites you to perform a programming exercise to solidify the lesson's ideas.

The Try It has several subsections. Lesson Requirements describes the exercise so you know what should happen. Hints gives pointers about possible confusing aspects of the problem, if they're needed. Step-by-Step provides a numbered series of steps that show how to solve the problem.

A screencast on the accompanying DVD shows me working through the Try It problem. Additional commentary at the end of the screencast highlights extensions of the lesson's main concepts.

After the Try It's Step-by-Step section, the lesson concludes with extra exercises that you can solve for further practice and to expand the lesson's main ideas. Some of the exercises extend the material in the main lesson, so I recommend that you at least skim the exercises and ask yourself if you think you could do them. Solutions to the Try Its and all of the exercises are available for download on the book's website. Additional screencasts show how to work through many of the exercises.

The one thing that a good classroom experience has that this book doesn't is direct interaction. You can't shout questions at the instructor, work in a team with fellow students, and discuss exercises with other students in the campus coffee house.

Although the book itself can't help here, you can do at least three things to get this kind of interaction. First, join the Wrox P2P (peer-to-peer) discussion forum for this book. As the section “P2P.WROX.COM” later in this lesson says, you can join the discussion forum to post questions, provide answers, see what other readers are doing with the book's material, and generally keep tabs on book-related topics.

You can also sign up for other discussion groups on the Internet, too. You can post questions on those discussions, but it's also very interesting to see what other people are asking. Book discussion groups often don't have as much traffic, so the topics tend to be more limited than those in these other groups. (Although I watch my P2P groups closely, so go there if you want me to answer.)

Finally, if you get stuck on an exercise or some other program you're working on, e-mail me at [email protected]. I won't solve the exercises for you but I'll try to clarify problems or give you the hints you need to solve them yourself.

How This Book Is Structured

This book is divided into seven sections, each containing a series of short lessons. The lessons are generally arranged in order, with later lessons depending on earlier ones, so you should study the lessons more or less in order, at least through the first four sections. The lessons in sections V, VI, and VII cover slightly more specialized topics and you can study them in any order.

Many of the exercises are tagged with a topic as in [Games] or [WPF]. Those indicate a theme that you may find interesting. For example, the [Games] exercises involve techniques that you may find useful if you want to build game programs. The topics include:

  • [WPF]—These ask you to use WPF. They are often harder than corresponding Windows Forms programs, but they sometimes produce better-looking results. (You also need to use WPF to build tablet-style and Windows Phone apps.)
  • [Games]—These are generally amusing or demonstrate techniques that may be useful in building game programs.
  • [SimpleEdit]—This is a simple word processing application that is built and enhanced over a sequence of exercises in several lessons.
  • [Drawing]—These exercises make a program that draws lines and shapes.
  • [Hard]—Exercises with this tag are generally harder than most of the other exercises so they may take some extra time. (I bet you guessed that!)
  • [Advanced]—These exercises use more advanced techniques and may be harder.
  • [Bonus]—These exercises extend the topic covered in the lesson and include extra instructions for performing a technique not covered in the main lesson.

The book's sections are:

  • I: The Visual Studio IDE and Controls—These lessons explain how to use the Visual Studio integrated development environment (IDE) and how to use the controls that make up a user interface. You need to study these lessons to get started.
  • II: Variables and Calculations—These lessons deal with variables and calculations. They explain what variables are and how a program can use them to calculate results. They also explain how to debug programs.
  • III: Program Statements—These lessons describe program statements and syntax. They explain how to control the program's flow, make decisions, and repeat operations.
  • IV: Classes—These lessons deal with classes. They explain how to create and use classes and how to use more advanced class features such as generics and operator overloading.
  • V: System Interactions—These lessons explain ways in which a program can interact with the operating system by reading and writing files and by generating printouts.
  • VI: Windows Apps—These sections explain how you can build Windows Store and Windows Phone apps.
  • VII: Specialized Topics—These lessons introduce topics that don't fit well in the other sections. They explain how to localize programs for different parts of the world, how to build simple database programs, and how to use Language Integrated Query (LINQ) to manipulate data in objects and databases.

What You Need to Use This Book

To get the most out of this book, you need to install Visual Studio and C#. You don't need any fancy version of Visual Studio or C# Professional Edition. In fact, Visual Studio Professional and the other full-featured versions don't really add all that much that you're likely to want to use for a long time. Mostly they add support for performing unit tests, managing test cases, profiling code, building code libraries, and performing other tasks that are more useful for programming teams than they are for individuals.

To work through this book, the Community Edition should be good enough. (And it's free!)

The following list describes some links that you may find useful for learning about and installing different Visual Studio products:

At a minimum, visit the Visual Studio Express Edition page (www.visualstudio.com/products/visual-studio-express-vs) and download and install Visual Studio Community Edition.

Running any version of Visual Studio will require that you have a reasonably fast, modern computer with a large hard disk and lots of memory. For example, I'm fairly happy running my Intel Core 2 system at 1.60 GHz with 8 GB of memory and a huge 1 TB hard drive. (That's a lot more disk space than necessary but disk is relatively cheap.)

Conventions

To help you get the most from the text and keep track of what's happening, we've used several conventions throughout the book.

As for styles in the text:

  • New terms and important words are highlighted when they are introduced.
  • Keyboard strokes look like this: Ctrl+A.
  • Code, URLs, and e-mail addresses within the text are shown in monofont type as in x = 10, www.vb-helper.com, and [email protected].
  • Code snippets are shown in a monofont type like this.

The code editor in Visual Studio provides a rich color scheme to indicate various parts of code syntax such as variables, comments, and C# keywords. That's a great tool to help you learn language features in the editor and to help prevent mistakes as you code, but the colors don't show up in the book.

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. (I like to type in the code because it helps me focus on it so I get a better understanding.)

Many of the examples 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. If you get stuck, e-mail me or download the solution from the book's web page.

All of the source code used in this book is available for download on the book's website. Any updates to the code will be posted there.

Errata

The Wrox editors and I 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 our books, like a spelling mistake or faulty piece of code, we would be very 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 us 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 on the 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 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, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will 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 on 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 wish 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.

Once 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 would like to have new messages from a particular forum e-mailed to you, click on the “Subscribe to this Forum” icon by the forum name in the forum listing.

For more information about how to use 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 on 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 in the forums, feel free to e-mail me at [email protected]. I can't promise to solve every problem but I'll try to help you out if I can.

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

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