Chapter 1
Your First Taste of C#
Developing a C# program involves writing code, compiling it into Common Intermediate Language (CIL) code, and running the CIL code. This is a process you will repeat again and again during your career as a C# programmer. It is crucial that you feel comfortable with it. Therefore, the main objective of this chapter is to give you the opportunity to experience the process of software development in C# using Visual Studio Community 2015, a free Integrated Development Environment (IDE) from Microsoft.
As it is important to write code that not only works but that is also easy to read and maintain, this chapter introduces you to C# code conventions. Examples for this and the following chapters have been developed using Visual Studio Community 2015.
This section highlights steps in C# development: writing the program, compiling it into CIL code and running the CIL code. You will be using Visual Studio Community 2015, which can be downloaded for free from Microsoft’s website. If you have not done so, please install Visual Studio Community 2015 by following the instructions in Appendix A, “Visual Studio Community 2015.”
Start Visual Studio Community 2015. When opened, you’ll see something similar to Figure 1.1. If the software won’t open because you have not registered, do register now. Registration is free and explained in Appendix A, “Visual Studio Community 2015.”
Figure 1.1: Starting Visual Studio Community 2015
Click the New Project icon to create a new project and select Console Application (See Figure 1.2).
Figure 1.2: Creating a new project
Accept ConsoleApplication1 as the solution and project names, then click the OK button. You’ll see a project and a solution created for you like in Figure 1.3. Better still, Visual Studio Community 2015 also creates a program file with some boilerplate code like the one in Figure 1.3. Note that a project is a container to easily manage your application. It contains C# source files and other resources such as image and video files and documentation that describes your application. When you create a project, Visual Studio Community 2015 also creates a solution. A solution is yet another container that may contain one or more projects.
Now you’re ready to program.
Figure 1.3: The created solution and project
Insert the following two lines after the opening curly bracket after static void Main(string[] args):
Console.WriteLine("Hello World!");
Console.ReadLine();
Listing 1.1 shows your complete program with recently inserted lines printed in bold.
Listing 1.1: A simple C# program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
}
Alternatively, you can double-click on the solution (.sln) file in the zip file accompanying this book that you can download from the book’s download site.
Developing with Visual Studio Community 2015 is really easy. To compile your program, simply press the F5 key or click the Start button on the toolbar. The Start button is green and is shown in Figure 1.4.
Figure 1.4: The Start button
If your program compiled successfully, Visual Studio Community 2015 will also run your program. As a result, you’ll see a console with the text “Hello World!” See Figure 1.5.
Figure 1.5: Running your program
Congratulations. You have successfully written your first C# program. Type Enter to close the console after you finish admiring your first program. Since the sole aim of this chapter is to familiarize yourself with the writing and compiling process, I will not be attempting to explain how the program works.
It is important to write correct C# programs that run. However, it is also crucial to write programs that are easy to read and maintain. It is believed that eighty percent of the lifetime cost of a piece of software is spent on maintenance. Also, the turnover of programmers is high, thus it is very likely that someone other than you will maintain your code during its lifetime. Whoever inherits your code will appreciate clear and easy-to-read program sources.
Using consistent code conventions is one way to make your code easier to read. (Other ways include proper code organization and sufficient commenting.) Code conventions include filenames, file organization, indentation, comments, declaration, statements, white space, and naming conventions. Microsoft has published a document that outlines standards that its employees should follow. The document can be viewed here.
http://msdn.microsoft.com/en-us/library/ff926074.aspx
Program samples in this book will follow the recommended conventions outlined in this document. I’d also like to encourage you to develop the habit of following these conventions starting the first day of your programming career, so that writing clear code comes naturally at a later stage.
This chapter helped you write your first C# program using Visual Studio Community 2015. You successfully wrote, compiled, and ran your program.
18.221.222.47