Here are a couple of excerpts from the book. Enjoy!
Microsoft Visual C# is a powerful but simple language aimed primarily at developers creating applications by using the Microsoft .NET Framework. It inherits many of the best features of C++ and Microsoft Visual Basic, but few of the inconsistencies and anachronisms, resulting in a cleaner and more logical language. C# 1.0 made its public debut in 2001. The advent of C# 2.0 with Visual Studio 2005 saw several important new features added to the language, including Generics, Iterators, and anonymous methods. C# 3.0 which was released with Visual Studio 2008, added extension methods, lambda expressions, and most famously of all, the Language Integrated Query facility, or LINQ. The latest incarnation of the language, C# 4.0, provides further enhancements that improve its interoperability with other languages and technologies. These features include support for named and optional arguments, the dynamic type which indicates that the language runtime should implement late binding for an object, and variance which resolves some issues in the way in which generic interfaces are defined. C# 4.0 takes advantage of the latest version of the .NET Framework, also version 4.0. There are many additions to the .NET Framework in this release, but arguably the most significant are the classes and types that constitute the Task Parallel Library (TPL). Using the TPL, you can now build highly scalable applications that can take full advantage of multi-core processors quickly and easily. The support for Web services and Windows Communication Foundation (WCF) has also been extended; you can now build services that follow the REST model as well as the more traditional SOAP scheme.
The development environment provided by Microsoft Visual Studio 2010 makes all these powerful features easy to use, and the many new wizards and enhancements included in Visual Studio 2010 can greatly improve your productivity as a developer.
Who This Book Is For
This book assumes that you are a developer who wants to learn the fundamentals of programming with C# by using Visual Studio 2010 and the .NET Framework version 4.0. In this book, you will learn the features of the C# language, and then use them to build applications running on the Microsoft Windows operating system. By the time you complete this book, you will have a thorough understanding of C# and will have used it to build Windows Presentation Foundation applications, access Microsoft SQL Server databases by using ADO.NET and LINQ, build responsive and scalable applications by using the TPL, and create REST and SOAP Web services by using WCF.
Chapter 1 (excerpt)
Welcome to C#
After completing this chapter, you will be able to:
· Use the Microsoft Visual Studio 2010 programming environment.
· Create a C# console application.
· Explain the purpose of namespaces.
· Create a simple graphical C# application.
Microsoft Visual C# is Microsoft’s powerful component-oriented language. C# plays an important role in the architecture of the Microsoft .NET Framework, and some people have compared it to the role that C played in the development of UNIX. If you already know a language such as C, C++, or Java, you’ll find the syntax of C# reassuringly familiar. If you are used to programming in other languages, you should soon be able to pick up the syntax and feel of C#; you just need to learn to put the braces and semicolons in the right place. I hope this is just the book to help you!
In Part I, you’ll learn the fundamentals of C#. You’ll discover how to declare variables and how to use arithmetic operators such as the plus sign (+) and minus sign (–) to manipulate the values in variables. You’ll see how to write methods and pass arguments to methods. You’ll also learn how to use selection statements such as if and iteration statements such as while. Finally, you’ll understand how C# uses exceptions to handle errors in a graceful, easy-to-use manner. These topics form the core of C#, and from this solid foundation, you’ll progress to more advanced features in Part II through Part VI.
Beginning Programming with the Visual Studio 2010 Environment
Visual Studio 2010 is a tool-rich programming environment containing the functionality that you need to create large or small C# projects. You can even construct projects that seamlessly combine modules written by using different programming languages such as C++, Visual Basic, and F#. In the first exercise, you will open the Visual Studio 2010 programming environment and learn how to create a console application.
Note A console application is an application that runs in a command prompt window rather than providing a graphical user interface.
Create a console application in Visual Studio 2010
· If you are using Visual Studio 2010 Standard or Visual Studio 2010 Professional, perform the following operations to start Visual Studio 2010:
1. On the Microsoft Windows task bar, click the Start button, point to All Programs, and then point to the Microsoft Visual Studio 2010 program group.
2. In the Microsoft Visual Studio 2010 program group, click Microsoft Visual Studio 2010.
Visual Studio 2010 starts, like this:
Note If this is the first time you have run Visual Studio 2010, you might see a dialog box prompting you to choose your default development environment settings. Visual Studio 2010 can tailor itself according to your preferred development language. The various dialog boxes and tools in the integrated development environment (IDE) will have their default selections set for the language you choose. Select Visual C# Development Settings from the list, and then click the Start Visual Studio button. After a short delay, the Visual Studio 2010 IDE appears.
· If you are using Visual C# 2010 Express, on the Microsoft Windows task bar, click the Start button, point to All Programs, and then click Microsoft Visual C# 2010 Express.
Visual C# 2010 Express starts, like this:
Note If this is the first time you have run Visual C# 2010 Express, you might see a dialog box prompting you to choose your default development environment settings. Select Expert Settings from the list, and then click the Start Visual Studio button. After a short delay, the Visual C# 2010 IDE appears.
Note To avoid repetition, throughout this book I simply state, “Start Visual Studio” when you need to open Visual Studio 2010 Standard, Visual Studio 2010 Professional, or Visual C# 2010 Express. Additionally, unless explicitly stated, all references to Visual Studio 2010 apply to Visual Studio 2010 Standard, Visual Studio 2010 Professional, and Visual C# 2010 Express.
· If you are using Visual Studio 2010 Standard or Visual Studio 2010 Professional, perform the following tasks to create a new console application:
1. On the File menu, point to New, and then click Project.
The New Project dialog box opens. This dialog box lists the templates that you can use as a starting point for building an application. The dialog box categorizes templates according to the programming language you are using and the type of application.
2. In the left pane, under Installed Templates, click Visual C#. In the middle pane, verify that the combo box at the top of the pane displays the text .NET Framework 4.0, and then click the Console Application icon. You might need to scroll the middle pane to see the Console Application icon.
3. In the Location field, if you are using Windows Vista type C:\Users\YourName\Documents\Microsoft Press\Visual CSharp Step By Step\Chapter 1. If you are using Windows 7, type C:\Users\YourName\My Documents\Microsoft Press\Visual CSharp Step By Step\Chapter 1. Replace the text YourName in these paths with your Windows user name.
Note To save space throughout the rest of this book, I will simply refer to the path
“C:\Users\YourName\Documents” or “C:\Users\YourName\My Documents” as your Documents folder.
Tip If the folder you specify does not exist, Visual Studio 2010 creates it for you.
4. In the Name field, type TextHello.
5. Ensure that the Create directory for solution check box is selected, and then click OK.
· If you are using Visual C# 2010 Express, perform the following tasks to create a new console application:
1. On the File menu, click New Project.
2. In the New Project dialog box, in the middle pane click the Console Application icon.
3. In the Name field, type TextHello.
4. Click OK.
Visual C# 2010 Express saves solutions to the C:\Users\YourName\AppData\Local\Temporary Projects folder by default. You can specify an alternative location when you save the solution.
5. On the File menu, click Save TextHello As.
6. In the Save Project dialog box, in the Location field specify the Microsoft Press\Visual CSharp Step By Step\Chapter 1 folder under your Documents folder.
7. Click Save.
Visual Studio creates the project using the Console Application template and displays the starter code for the project, like this:
The menu bar at the top of the screen provides access to the features you’ll use in the programming environment. You can use the keyboard or the mouse to access the menus and commands exactly as you can in all Windows-based programs. The toolbar is located beneath the menu bar and provides button shortcuts to run the most frequently used commands.
The Code and Text Editor pane occupying the main part of the IDE displays the contents of source files. In a multifile project, when you edit more than one file, each source file has its own tab labeled with the name of the source file. You can click the tab to bring the named source file to the foreground in the Code and Text Editor window. The Solution Explorer pane (on the right side of the dialog box) displays the names of the files associated with the project, among other items. You can also double-click a file name in the Solution Explorer pane to bring that source file to the foreground in the Code and Text Editor window.
Before writing the code, examine the files listed in Solution Explorer, which Visual Studio 2010 has created as part of your project:
· Solution ‘TextHello’ This is the top-level solution file, of which there is one per application. If you use Windows Explorer to look at your Documents\Microsoft Press\Visual CSharp Step By Step\Chapter 1\TextHello folder, you’ll see that the actual name of this file is TextHello.sln. Each solution file contains references to one or more project files.
· TextHello This is the C# project file. Each project file references one or more files containing the source code and other items for the project. All the source code in a single project must be written in the same programming language. In Windows Explorer, this file is actually called TextHello.csproj, and it is stored in the \Microsoft Press\Visual CSharp Step By Step\Chapter 1\TextHello\TextHello folder under your Documents folder.
· Properties This is a folder in the TextHello project. If you expand it, you will see that it contains a file called AssemblyInfo.cs. AssemblyInfo.cs is a special file that you can use to add attributes to a program, such as the name of the author, the date the program was written, and so on. You can specify additional attributes to modify the way in which the program runs. Learning how to use these attributes is outside the scope of this book.
· References This is a folder that contains references to compiled code that your application can use. When code is compiled, it is converted into an assembly and given a unique name. Developers use assemblies to package useful bits of code they have written so that they can distribute it to other developers who might want to use the code in their applications. Many of the features that you will be using when writing applications using this book make use of assemblies provided by Microsoft with Visual Studio 2010.
· App.config This is the application configuration file. You can specify settings that your application can use at runtime to modify its behavior, such as the version of the .NET Framework to use to run the application. You will learn more about this file in later chapters in this book.
· Program.cs This is a C# source file and is the one currently displayed in the Code and Text Editor window when the project is first created. You will write your code for the console application in this file. It also contains some code that Visual Studio 2010 provides automatically, which you will examine shortly.