{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8aa5f2a1",
   "metadata": {},
   "source": [
    "(lab-edit-compile-run)=\n",
    "\n",
    "# Lab 01\n",
    "\n",
    "Your goals for this lab include:\n",
    "\n",
    "1. To setup the development tools needed for C# programming.\n",
    "2. To create your first .NET project for C# development using the terminal.\n",
    "\n",
    "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 [^footnote-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:\"\n",
    "\n",
    "```csharp\n",
    "Print the words\n",
    "hello, world.\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a6e24bb",
   "metadata": {},
   "source": [
    "## Installing  Toolchain\n",
    "\n",
    "### VS Code\n",
    "\n",
    "   Visit the Visual Studio Code [website](https://code.visualstudio.com/) site or use a package manager\n",
    "   to install the current version of VS Code for your operating system. [^footnote-2] An [online version](https://vscode.dev) of VS Code is also available.\n",
    "\n",
    "### .NET SDK\n",
    "\n",
    "   Visit the [.NET page](https://code.visualstudio.com/docs/languages/dotnet) and scroll down to click on\n",
    "   the \"Install the .NET SDK\" button. You will be redirected to the\n",
    "   [.NET Download](https://dotnet.microsoft.com/en-us/download) page, with the OS, architect, and the recent\n",
    "   long term support version of .NET SDK pre-selected for you to start downloading.\n",
    "   The download page also includes instructions for the installation and verification of the SDK.\n",
    "   At the end of the installation, you should see a notification that the .NET SDK, along with\n",
    "   .NET runtime, ASP.NET Core Runtime, and .NET Windows Desktop Runtime are installed.\n",
    "\n",
    "   :::{figure} ../../images/dotnet_sdk_installation.jpg\n",
    "   :scale: 25%\n",
    "   :::\n",
    "\n",
    "   To verify if .NET SDK is installed correctly, open a new terminal (Windows PowerShell or\n",
    "   macOS Terminal) and run the `dotnet` command by typing `dotnet` at the command prompt and then\n",
    "   hit the Enter key. If .NET is correctly installed, you should see results as below.\n",
    "\n",
    "   :::{figure} ../../images/dotnet_install_verification.jpg\n",
    "   :scale: 25%\n",
    "\n",
    "   Outcome of running the `dotnet` command in terminal\n",
    "   :::\n",
    "\n",
    "### C# Dev Kit extension\n",
    "\n",
    "   Click on the the Extension view icon on the Activity Bar, search and install the `C# Dev Kit` (this should\n",
    "   also install the `C# extension` from Microsoft)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e93dfa2",
   "metadata": {},
   "source": [
    "```{index} create project, project, C# project\n",
    "```\n",
    "\n",
    "(create-project)=\n",
    "\n",
    "## Creating a C# Project\n",
    "\n",
    "### Creating the Workspace Folder\n",
    "\n",
    "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.\n",
    "\n",
    ":::{note}\n",
    "   You can actually perform this task using your computer's GUI but this is an important practice, especially if you are new to the terminal/shell.\n",
    ":::\n",
    "\n",
    "   1.  Start your terminal application (Windows PowerSell or macOS Terminal): By default,\n",
    "       you would be in your **user home directory** with a command prompt when the terminal app starts.\n",
    "\n",
    "   2.  Issue the command `pwd` to show your present work path to make sure you are in your user home directory.\n",
    "       You should see the terminal showing your current location if you use Windows:\n",
    "\n",
    "       ```console\n",
    "       PS C:\\Users\\[username]> pwd              // type the command and Enter\n",
    "       Path\n",
    "       ----\n",
    "       C:\\Users\\[username]                      // [username] is the user account name and also the user home directory name\n",
    "       ```\n",
    "\n",
    "       or, for macOS:\n",
    "\n",
    "       ```console\n",
    "       [username]@mac:~$ pwd\n",
    "       /Users/[username]\n",
    "       ```\n",
    "\n",
    "   3.  Type the `ls` command + Enter to see the files and directories in your user home directory.\n",
    "       You should see the usual folder/directory names.\n",
    "\n",
    "   4.  Create a directory called \"workspace\" (`mkdir workspace`) in your user home directory if you have not done so.\n",
    "\n",
    "   5.  Change directory into the workspace directory (`cd workspace`).\n",
    "\n",
    "   6.  Inside workspace, create a directory **cscs** (`mkdir cscs`).\n",
    "\n",
    "   7.  Also inside workspace, create another directory called tests (`mkdir tests`) if you have not done so.\n",
    "\n",
    "   8.  `ls` + Enter to make sure that you have two directories created: cscs and tests.\n",
    "\n",
    "   9.  Change directory up one level (`cd ..`) or just `cd ~` to go back to the your user home directory.\n",
    "\n",
    "   10. Use the `exit` command (`exit` + Enter) to leave the terminal.\n",
    "\n",
    "   The process should look like this:\n",
    "\n",
    "```bash\n",
    "   [username]@[computer]:~$ ls\n",
    "   Applications      Google Drive      Pictures\n",
    "   Desktop           Library           Zotero\n",
    "   Documents         Movies\n",
    "   Downloads         Music\n",
    "   [username]@[computer]:~$ mkdir workspace\n",
    "   [username]@[computer]:~$ cd workspace\n",
    "   [username]@[computer]:~/workspace$ mkdir cscs\n",
    "   [username]@[computer]:~/workspace$ mkdir tests\n",
    "   [username]@[computer]:~/workspace$ ls\n",
    "   cscs tests\n",
    "   [username]@[computer]:~/workspace$ cd ..\n",
    "   [username]@[computer]:~$ exit\n",
    "```\n",
    "\n",
    "\n",
    "### Creating the Project Folder\n",
    "\n",
    "   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:\n",
    "\n",
    "   1. Start the terminal app: You start the terminal application (Windows PowerSell or macOS Terminal). You should\n",
    "      be in your user home directory by default.\n",
    "\n",
    "   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`.\n",
    "\n",
    "   3. Make sure you are in *tests*: `pwd` + Enter and you should see that you are in the tests directory in *USERNAME*/workspace/tests.\n",
    "\n",
    "   4. Create the project folder (\"Hello\" in this case): `mkdir Hello`.\n",
    "\n",
    "   5. Make sure you have created the folder: `ls` + Enter and you should see the Hello directory.\n",
    "\n",
    "   6. Now you need to change into the Hello project directory: `cd Hello`.\n",
    "\n",
    "   7. Check your location: Issue the `pwd` + Enter command and you should be in the Hello project\n",
    "      directory:\n",
    "\n",
    "      ```console\n",
    "      [username]/workspace/tests/Hello$\n",
    "      ```\n",
    "\n",
    "   The whole process would look like this:\n",
    "\n",
    "```bash\n",
    "   [username]@[computer]:~/$ cd workspace\n",
    "   [username]@[computer]:~/workspace$ cd tests\n",
    "   [username]@[computer]:~/workspace/tests$ pwd\n",
    "   /home/[username]/workspace/tests/\n",
    "   [username]@[computer]:~/workspace/tests$ mkdir Hello\n",
    "   [username]@[computer]:~/workspace/tests$ ls\n",
    "   Hello\n",
    "   [username]@[computer]:~/workspace/tests$ cd Hello\n",
    "   [username]@[computer]:~/workspace/tests/Hello$ pwd\n",
    "   /home/[username]/workspace/tests/Hello$\n",
    "   [username]@[computer]:~/workspace/tests/Hello$\n",
    "```\n",
    "\n",
    "\n",
    "### Creating a C# Project\n",
    "\n",
    "   You just created the project folder (Hello) and now you are ready to create the **project** following\n",
    "   the steps as follows.\n",
    "\n",
    "   1. Make sure you are in your Hello project directory. (`pwd` should show that you are\n",
    "      in the Hello directory:\n",
    "\n",
    "      ```console\n",
    "      [username]/workspace/tests/Hello$\n",
    "      ```\n",
    "\n",
    "   2. Issue the command `dotnet new console` + Enter to create the new project.\n",
    "\n",
    "   3. Run the project by issuing the `dotnet run` command.\n",
    "\n",
    "```bash\n",
    "   [username]@[computer]:~/workspace/tests/Hello$ dotnet new console\n",
    "   The template \"Console App\" was created successfully.\n",
    "\n",
    "   Processing post-creation actions...\n",
    "   Restoring /Users/[username]/workspace/tests/Hello/Hello.csproj:\n",
    "      Determining projects to restore...\n",
    "      Restored /Users/[username]/workspace/tests/Hello/Hello.csproj (in 145 ms).\n",
    "   Restore succeeded.\n",
    "\n",
    "   [username]@[computer]:~/workspace/tests/Hello$ dotnet run\n",
    "   Hello, World!\n",
    "   [username]@[computer]:~/workspace/tests/Hello$\n",
    "```\n",
    "\n",
    "\n",
    "   Congratulations! You have just created and run your first C# console application project!\n",
    "\n",
    "### Using VS Code\n",
    "\n",
    "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:\n",
    "\n",
    "   1. In the terminal, change into the project directory if you have not done so: (*USERNAME*/workspace/tests/Hello\\$)\n",
    "\n",
    "   2. Start VS Code by issuing the command `code .` (`code` is VS Code and the `.` means the present location).\n",
    "      (If VS Code is not launching after your issue `code .`, it may be a PATH issue.\n",
    "      See this VS Code [Command Line Interface (CLI) page](https://code.visualstudio.com/docs/editor/command-line#_launching-from-command-line) for a solution).\n",
    "\n",
    "   3. VS Code will start with the folder as the working directory shown in the VS Code Explorer view.\n",
    "\n",
    "   4. Click on the file `Program.cs` to see the code.\n",
    "\n",
    "   5. Change \"`Hello, World!`\" to \"`Hello, YOUR_NAME!`\".\n",
    "\n",
    "   6. Click on the **Toggle Panel** icon (<svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"><path d=\"M15 12.5C15 13.881 13.881 15 12.5 15H3.5C2.119 15 1 13.881 1 12.5V3.5C1 2.119 2.119 1 3.5 1H12.5C13.881 1 15 2.119 15 3.5V12.5ZM2 10H14V3.5C14 2.672 13.328 2 12.5 2H3.5C2.672 2 2 2.672 2 3.5V10Z\"/></svg>) to show the VS Code panel:\n",
    "\n",
    "      - Click on the terminal tab ({{ vscode_terminal }}) in the bottom Panel pane.\n",
    "      - The terminal here is exactly the same as the terminal in your terminal app.\n",
    "      - The current directory is by default your project (`Hello`) directory since you started `code .` from there.\n",
    "\n",
    "   7. Issue command `dotnet run` at the terminal to see the result:\n",
    "\n",
    "      ```\n",
    "      \"Hello, YOUR_NAME!\".\n",
    "      ```\n",
    "\n",
    "      Congratulations! You have just **modified** and run your first C# console application project!\n",
    "\n",
    "```{rubric} Footnotes\n",
    "```\n",
    "\n",
    "[^footnote-1]: 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](https://brew.sh/) for macOS, [Chocolatey](https://chocolatey.org/) for Windows, and apt/snap for Ubuntu Linux.\n",
    "\n",
    "[^footnote-2]: Wikipedia Contributors. (2024, July 25). “Hello, World!” program. Wikipedia; Wikimedia Foundation. <https://en.wikipedia.org/wiki/%22Hello,_World!%22_program>"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
