1.5. Lab 01

Your goals for this lab include:

  1. To setup the development tools needed for C# programming.

  2. To create your first .NET project for C# development using the terminal.

A “hello, world” program is traditionally a simple computer program that outputs a test message similar to “Hello, World!” to show that the program’s basic syntax works. Nowadays, it is used as the first program when learning a new programming language. The origin of this convention is known to be from the 1978 book The C Programming Language by Brian Kernighan and Dennis Ritchie, which in turns is inherited from a 1974 Bell Laboratories internal memorandum by Brian Kernighan, Programming in C: A Tutorial [1]. In the book, it says “[t]he only ways to learn a new programming language is by writing programs in it. The first program to write is the same for all languages:”

Print the words
hello, world.

1.5.1. Install VS Code, .NET SDK, and C# Dev Kit Extension

  1. Install VS Code:

    Visit the Visual Studio Code website site or use a package manager to install the current version of VS Code for your operating system. [2] An online version of VS Code is also available.

  2. Install .NET SDK:

    Visit the .NET page and scroll down to click on the “Install the .NET SDK” button. You will be redirected to the .NET Download page, with the OS, architect, and the recent long term support version of .NET SDK pre-selected for you to start downloading. The download page also includes instructions for the installation and verification of the SDK. At the end of the installation, you should see a notification that the .NET SDK, along with .NET runtime, ASP.NET Core Runtime, and .NET Windows Desktop Runtime are installed.

    ../_images/dotnet_sdk_installation.jpg

    To verify if .NET SDK is installed correctly, open a new terminal (Windows PowerShell or macOS Terminal) and run the dotnet command by typing dotnet at the command prompt and then hit the Enter key. If .NET is correctly installed, you should see results as below.

    ../_images/dotnet_install_verification.jpg

    Outcome of running the dotnet command in terminal

  3. Install the C# Dev Kit extension:

    Click on the the Extension view icon on the Activity Bar, search and install the C# Dev Kit (this should also install the C# extension from Microsoft).

1.5.2. Create a C# Project

  1. Create workspace folders:

    Follow the following steps at the terminal to prepare the environment for learning C#. You will 1). first create a directory for your project and then 2). turn the directory into a project directory.

    Note

    You can perform this task using your computer’s GUI but this is an important practice, especially if you are new to the terminal/shell.

    1. Start your terminal application (Windows PowerSell or macOS Terminal): By default, you would be in your user home directory with a command prompt when the terminal app starts.

    2. Issue the command pwd to show your present work path to make sure you are in your user home directory. You should see the terminal showing your current location if you use Windows:

      PS C:\Users\tcn85> pwd              // type the command and Enter
      Path
      ----
      C:\Users\tcn85                      // tcn85 is my user account name and also the user home directory name
      

      or, for macOS:

      tcn85@mac:~$ pwd
      /Users/tcn85
      
    3. Type the ls command + Enter to see the files and directories in your user home directory. You should see the usual folder/directory names.

    4. Create a directory called “workspace” (mkdir workspace) in your user home directory if you have not done so.

    5. Change directory into the workspace directory (cd workspace).

    6. Inside workspace, create a directory introcscs (mkdir introcscs).

    7. Also inside workspace, create another directory called tests (mkdir tests) if you have not done so.

    8. ls + Enter to make sure that you have two directories created: introcscs and tests.

    9. Change directory up one level (cd ..) or just cd ~ to go back to the your user home directory.

    10. Use the exit command (exit + Enter) to leave the terminal.

    The process should look like this:

    tychen@mac:~$ ls
    Applications      Google Drive      Pictures
    Desktop           Library           Zotero
    Documents         Movies
    Downloads         Music
    tychen@mac:~$ mkdir workspace
    tychen@mac:~$ cd workspace
    tychen@mac:~/workspace$ mkdir introcscs
    tychen@mac:~/workspace$ mkdir tests
    tychen@mac:~/workspace$ ls
    introcscs tests
    tychen@mac:~/workspace$ cd ..
    tychen@mac:~$ exit
    
  2. Create and change directory into a project folder:

    Here you will create a folder for a test project, call it Hello. The specific process should look as the code block below and the steps are explained first here:

    1. Start the terminal app: You start the terminal application (Windows PowerSell or macOS Terminal). You should be in your user home directory by default.

    2. Change directory (cd) into the tests directory: The tests directory is inside your workspace directory, so you have to change into workspace from your home directory first (cd workspace) then cd tests; or, you can issue cd workspace/tests.

    3. Make sure you are in tests: pwd + Enter and you should see that you are in the tests directory in USERNAME/workspace/tests.

    4. Create the project folder (“Hello” in this case): mkdir Hello.

    5. Make sure you have created the folder: ls + Enter and you should see the Hello directory.

    6. Now you need to change into the Hello project directory: cd Hello.

    7. Check your location: Issue the pwd + Enter command and you should be in the Hello project directory:

      USERNAME/workspace/tests/Hello$
      

    The whole process would look like this:

    tychen@mac:~/$ cd workspace
    tychen@mac:~/workspace$ cd tests
    tychen@mac:~/workspace/tests$ pwd
    /home/tcn85/workspace/tests/
    tychen@mac:~/workspace/tests$ mkdir Hello
    tychen@mac:~/workspace/tests$ ls
    Hello
    tychen@mac:~/workspace/tests$ cd Hello
    tychen@mac:~/workspace/tests/Hello$ pwd
    /home/tcn85/workspace/tests/Hello$
    tychen@mac:~/workspace/tests/Hello$
    
  3. Create and execute the .NET C# console app project:

    You just created the project folder (Hello) and now you are ready to create the project following the steps as follows.

    1. Make sure you are in your Hello project directory. (pwd should show that you are in the Hello directory:

      USERNAME/workspace/tests/Hello$
      
    2. Issue the command dotnet new console + Enter to create the new project.

    3. Run the project by issuing the dotnet run command.

    tychen@mac:~/workspace/tests/Hello$ dotnet new console
    The template "Console App" was created successfully.
    
    Processing post-creation actions...
    Restoring /Users/tychen/workspace/tests/Hello/Hello.csproj:
       Determining projects to restore...
       Restored /Users/tychen/workspace/tests/Hello/Hello.csproj (in 145 ms).
    Restore succeeded.
    
    tychen@mac:~/workspace/tests/Hello$ dotnet run
    Hello, World!
    tychen@mac:~/workspace/tests/Hello$
    

    Congratulations! You have just created and run your first C# console application project!

  4. Start VS Code from terminal to edit and execute the project

    While we usually (and will in the future!) start VS Code by clicking on the application icon in your device, for now, follow the steps below:

    1. In the terminal, change into the project directory if you have not done so: (USERNAME/workspace/tests/Hello$)

    2. Start VS Code by issuing the command code . (code is VS Code and the . means the present location). (If VS Code is not launching after your issue code ., it may be a PATH issue. See this VS Code Command Line Interface (CLI) page for a solution).

    3. VS Code will start with the folder as the working directory shown in the VS Code Explorer view.

    4. Click on the file Program.cs to see the code.

    5. Change “Hello, World!” to “Hello, YOUR_NAME!”.

    6. Click on the Toggle Panel icon (vscode-toggle) to show the VS Code panel:

      • Click on the TERMINAL tab (vscode-terminal) in the bottom Panel pane.

      • The terminal here is exactly the same as the terminal in your terminal app.

      • The current directory is by default your project directory since you started code . from there.

    7. Issue command dotnet run at the terminal to see the result:

      "Hello, YOUR_NAME!".
      

      Congratulations! You have just modified and run your first C# console application project!

Footnotes