When you click the ok button visual studio will

Info icon This preview shows pages 36–39. Sign up to view the full content.

View Full Document Right Arrow Icon
When you click the OK button, Visual Studio will create a new project , a collection of files that are used to build a program. C# projects always contain source code files, but they often include other types of files, such as bitmaps. This newly created project will contain a C# source file called Program.cs , which should be visible in Visual Studio’s text editor. In case you’re not following along in Visual Studio as you read this, the code is reproduced in Example 2-1 . By the way, there’s no particular significance to the filename Program.cs . Visual Studio doesn’t care what you call your source files; by convention, they have a .cs extension, short for C#, although even that’s optional. Figure 2-1. Visual Studio’s New Project dialog 12 | Chapter 2: Basic Programming Techniques
Image of page 36

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 2-1. The code in a freshly created console application using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { } } } This program doesn’t do anything yet. To turn it into the traditional first example, you’ll need to add one line of code. This will go in between the two lines that contain the most-indented pair of braces ( { and } ). The modified version is shown in Exam- ple 2-2 , with the new line in bold. Example 2-2. The traditional first example, “Hello, world” using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, world"); } } } This example is now ready to run. From the Debug menu select the Start Without Debugging item, or just press Ctrl-F5. The program will run, and because you’ve writ- ten a console application, a console window will open. The first line of this window will contain the text “Hello, world” and this will be followed by a prompt saying “Press any key to continue...” Once you’ve finished admiring the fruits of your creation, press a key to dismiss the window. Don’t use Debug Start Debugging or F5—this will run the application in Visual Studio’s debugging mode, which doesn’t keep the window open once the application has finished. That’s not helpful for this ex- ample, which will most likely run to completion and then close the window before you’ve had a chance to see the output. Getting Started | 13
Image of page 37
Now that we have a complete program, let’s look at the code to see what each part is for—all of the pieces are things you’ll deal with every time you write in C#. Starting from the top, Program.cs has several lines beginning with using : using System; using System.Collections.Generic; using System.Linq; using System.Text; These using directives help the C# compiler work out what external code this particular source file will be using. No code is an island—to get any useful work done, your programs will rely on other code. All C# programs depend on the .NET Framework class library, for example: the one line of code we added to our program uses the class library to display a message. Using directives can declare an intent to use classes from
Image of page 38

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 39
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern