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:
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.
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:
To make room for the new material, some of the old material had to go. This edition doesn't cover:
I'd love to include those topics and many others, but there just isn't room in a book of this size.
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.)
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.
Educators have known for many years that different people use different learning styles most effectively. Different students may learn best by:
(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 RodStephens@CSharpHelper.com
. 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.
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:
The book's sections are:
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:
www.visualstudio.com/products/compare-visual-studio-2015-products-vs.aspx
msdn.microsoft.com/vstudio
msdn.microsoft.com/vstudio/hh341490.aspx
www.visualstudio.com/products/free-developer-offers-vs
www.visualstudio.com/products/visual-studio-express-vs.aspx
www.visualstudio.com/downloads/download-visual-studio-vs.aspx
www.microsoft.com/express/vcsharp
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.)
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:
x = 10, www.vb-helper.com
, and RodStephens@CSharpHelper.com
.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.
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.
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.
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:
p2p.wrox.com
and click on the Register link.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 RodStephens@CSharpHelper.com
. I can't promise to solve every problem but I'll try to help you out if I can.
The lessons in this section of the book explain how to use the Visual Studio integrated development environment (IDE). They explain how to use the IDE to create forms, place controls on the forms, and set control properties. These lessons describe some of C#'s most useful controls and give you practice using them.
You can do practically all of this in the IDE without writing a single line of code! That makes C# a great environment for rapid prototyping. You can build a form, add controls, and run the program to see what it looks like without ever creating a variable, declaring a method, or getting stuck in an infinite loop.
The lessons in this section explain how to get that far. A few of these lessons show how to add a line or two of code to make a form more interesting, but for now the focus is on using the IDE to build forms and controls. Writing code (and fixing the inevitable bugs) comes later.