Create a simple console program in visual C#

What you need to do to prepare for this exercise:


For most of the exercises (and homework projects) that you work with in this class, you will normally be given a 'starter project', that contains everything you need to get you started on the exercise/homework assignment. However, it's good for you to know how to create a new project from scratch. Go through this exercise, in order to make sure that you're comfortable with creating, compiling, and running (executing) a C# program that you've created yourself.

It is possible to quickly and easily create several different types of applications using visual C#. We will start by creating what is called a console application -- the steps specified here are largely adapted from those found in your textbook in Chapter 2 – the major difference is that your textbook doesn't use the Visual Studio development environment, but instead uses the .Net SDK & the command line. While you're welcome to try out how the book does things, I'd strongly recommend that you familiarize yourself with VS, instead.

  1. Start Microsoft Visual Studio. It is located in the Start menu at

    All Programs > Microsoft Visual Studio 2017

    We’ll be using Microsoft's Visual Studio 2017 that this quarter. It is an integrated environment that allows you to edit, build (compile) and run C# programs. You’ll learn more about it as the quarter progresses.

  2. Create a new project. Select File > New > Project from the VS menu bar. This brings up the New Project window that allows you to select from various items to create, which should look like the one pictured below. In the (left-hand pane), you want to pick Installed, then Templates, then Visual C#. At this point you may need to look around a bit to find something that says Console App. One place to look is in .Net Core (pictured below). Another place is .Net Standard or Windows Classic Desktop.

    You also must type in a project name (such as "Lecture1_Exercise2") and select the directory location where you want the project to be stored. If you're working at school, you'll wants to store the project in your student folder (H:\, if available) or on your own removable media. Click Ok and you'll go to the next step.

  3. You just created C# project, and a C# files containing a nearly-blank program. Projects store information about all of the files and options that are used to create a C# program. Files are typically C# source files that have a .cs extension and contain executable C# statements, although there are other file types.

    At this point your program should look like the following; if your code looks different then copy and past this into the file so that the entire file looks exactly like the following:

        using System;
    
        namespace Lecture1_Exercise2
        {
            class Program
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("Hello World!");
                }
            }
        }
                                    

    You should get into the habit of saving or files often, so that you do not lose work for any reason (technically, once you build your program, Visual Studio will save your files for you, but it is still a good reflex to have)

  4. Select Build > Build Solution from the VS menu bar. This will create an executable program from the source file Program.cs.

  5. Select Debug > Start from the VS menu bar. The program will run and you will see a new window with the words “Hello, World!” displayed, which will immediately disappear. This is because you've told VS to start the program, using a program called a debugger. The debugger was never told to stop the program anywhere, so it ran the program, which printed hello, and then finished. Once the program finished, the window disappears.

    You should try selecting Debug > Start Without Debugging, in which case you'll note that the window won't disappear immediately. VS will run the program directly (without the help of the debugger), and when the program finishes (which is does immediately), will then wait for you to push a key before taking down the window.