.. index:: Visual Studio Code, VS Code labs; VS Code .. _lab-edit-compile-run: Lab 01 ==================================================== Your goals for this lab include: #. To setup the development tools needed for C# programming. #. 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 [#]_. 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:" .. code-block:: C Print the words hello, world. Install VS Code, .NET SDK, and C# Dev Kit Extension ---------------------------------------------------- #. 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. [#]_ An `online version `_ of VS Code is also available. #. 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. .. figure:: ../images/dotnet_sdk_installation.jpg :scale: 25% 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. .. figure:: ../images/dotnet_install_verification.jpg :scale: 25% Outcome of running the ``dotnet`` command in terminal #. 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). .. index:: create project, project, C# project .. _create-project: Create a C# Project ------------------------------------------- #. 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. #. 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. #. 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: .. code-block:: console 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: .. code-block:: console tcn85@mac:~$ pwd /Users/tcn85 #. Type the ``ls`` command + Enter to see the files and directories in your user home directory. You should see the usual folder/directory names. #. Create a directory called "workspace" (``mkdir workspace``) in your user home directory if you have not done so. #. Change directory into the workspace directory (``cd workspace``). #. Inside workspace, create a directory **introcscs** (``mkdir introcscs``). #. Also inside workspace, create another directory called tests (``mkdir tests``) if you have not done so. #. ``ls`` + Enter to make sure that you have two directories created: introcscs and tests. #. Change directory up one level (``cd ..``) or just ``cd ~`` to go back to the your user home directory. #. Use the ``exit`` command (``exit`` + Enter) to leave the terminal. The process should look like this: .. code-block:: bash 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 #. 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: #. Start the terminal app: You start the terminal application (Windows PowerSell or macOS Terminal). You should be in your user home directory by default. #. 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``. #. Make sure you are in *tests*: ``pwd`` + Enter and you should see that you are in the tests directory in *USERNAME*/workspace/tests. #. Create the project folder ("Hello" in this case): ``mkdir Hello``. #. Make sure you have created the folder: ``ls`` + Enter and you should see the Hello directory. #. Now you need to change into the Hello project directory: ``cd Hello``. #. Check your location: Issue the ``pwd`` + Enter command and you should be in the Hello project directory: .. code-block:: console USERNAME/workspace/tests/Hello$ The whole process would look like this: .. code-block:: bash 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$ #. 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. #. Make sure you are in your Hello project directory. (``pwd`` should show that you are in the Hello directory: .. code-block:: console USERNAME/workspace/tests/Hello$ #. Issue the command ``dotnet new console`` + Enter to create the new project. #. Run the project by issuing the ``dotnet run`` command. .. code-block:: console :emphasize-lines: 1, 10, 11 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! #. 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: #. In the terminal, change into the project directory if you have not done so: (*USERNAME*/workspace/tests/Hello$) #. 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). #. VS Code will start with the folder as the working directory shown in the VS Code Explorer view. #. Click on the file ``Program.cs`` to see the code. #. Change "``Hello, World!``" to "``Hello, YOUR_NAME!``". #. 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. #. 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! .. |vscode-toggle| image:: ../../source/images/vscode-toggle.jpg :height: 2ex .. |vscode-terminal| image:: ../../source/images/vscode-terminal.jpg :height: 2ex .. A VS Code project folder (workspace) has a ``settings.json`` file in the ``.vscode`` directory for you to .. further configure the project. Also, checking out the VS Code docs to `get started `_ with the .. first steps of learning how to use VS Code. .. #. Using VS Code for managing solutions and projects .. You are encouraged to create a *single solution for this course*, with all the projects .. that you create in the solution. We will first practice by creating a *solution* with .. a *project* in it. .. We will create our first "hello, world" app project in the "tests" (the solution) folder .. in your "workspace" folder by going through the following steps: .. #. In a newly opened VS Code window, click on the Explorer view and choose .. "Create .NET Project" and then "Console App" from the Command Palette dropdown menu. .. Alternatively, you may use Cmd-Shift-P, Win-Shift-P, or View --> Command Palette .. to bring up Command Palette, and then type .NET: New Project, then select .. "Console App" as your project template. .. .. figure:: ../images/create_dotnet_project.jpg .. :scale: 30% .. #. Choose the project directory. In our example here, **Open** the *tests* directory .. that we created in the workspace directory under the user home directory. .. #. Choose a name for your project. In this example, type hello1 as the project name .. and Enter to confirm the name and Enter again to confirm the project path. .. You should see the hello1 project created along with a test.sln solution file. .. .. figure:: ../images/hello1_project_created.jpg .. :scale: 30% .. #. Click to expand the hello1 project directory and you should see the Program.cs file. Click to open the file in the editor. .. .. figure:: ../images/hello1_program_cs.jpg .. :scale: 30% .. #. To run the hello1 app, you have to run it as part of a project. From the menu bar, choose Run --> Run without Debugging. Alternatively, you can run the app by choosing the "Run project associated with this file" option from the Run Code icon (the ▷ right-pointing triangle in the upper right corner of the editor menu bar). .. When run successfully, you should see the building process and the .. code execution result in the TERMINAL panel: .. .. code-block:: bash .. tychen@mac:~/workspace/tests$ /Users/tychen/.vscode/extensions/ .. ms-dotnettools.csharp-2.39.29-darwin-x64/.debugger/x86_64/vsdbg .. --interpreter=vscode --connection=/var/folders/6t/bfp06fh96wn60n_mjtxmbhfm0000gn/T/ .. CoreFxPipe_vsdbg-ui-3e9ba55f636d4549b58b7e6499b27762 .. Hello, World! .. .. figure:: ../images/hello1_world.jpg .. :scale: 35% .. .. note:: .. If you click on Run Code triangle, you may see an error message in the OUTPUT .. panel. Just use the terminal : .. Sometimes using VS Code to run projects can be tricky. For now, let us use the terminal .. to run our C# projects as follows. .. As you can see, the outcome of running project hello2 is the same as project .. hello1. .. rubric:: Footnotes .. [#] For reasons such as handling versions, managing dependencies, and uninstallation, it is suggested that, when possible, you should use a package manager when installing software applications. Common used package managers incluce, e.g., `Homebrew `_ for macOS, `Chocolatey `_ for Windows, and apt/snap for Ubuntu Linux. .. [#] Wikipedia Contributors. (2024, July 25). “Hello, World!” program. Wikipedia; Wikimedia Foundation. https://en.wikipedia.org/wiki/%22Hello,_World!%22_program