C#, Hello World and command line (Windows)

This task assumes that you have installed at least the text editor and Rider (or Visual Studio) from the course.

1. What's the command line?

If you are not familiar with the command line, the first thing you should definitely check out is the command line instructions at :

Above is a description of how to open the command line, and the most common commands for Windows, Linux and Mac. Below is a more detailed course on how to use Bash.

In this fall's 2024 exercise, we'll be using the Git Bash interface. If you want to learn how to do the same thing with CMD Prompt or PowerShell, see fall 2020 material.

2. In micro classes: attaching the N-drive

This section applies to Agora microclasses. The N-drive does not work directly in home machines, but you don't necessarily need it!

Open this if you are in a micro class and use micro class machines

# mkdir

3. Creating and changing a directory

On Windows, first open Git Bash:

  • in the bottom left corner of the window take the image and type Git
  • you should see Git Bash
  • you should right click on this and pin it to the taskbar
  • open Git Bash

On Mac and Linux, the first thing to do is to open the Terminal.

Now, depending on your system, create a directory for yourself for this task.

In Agora' s microclasses, the directory must be under MyTemp. And since the same directory is used by many others, you have to add your own ID to the directory name (so don't write the following anywhere yet, but see what name will be used in the future):

c:\MyTemp\Anonymous\kurssit\ohj1\paate\paate1  (microclass computer, Windows muoto)
/c/MyTemp/Anonymous/kurssit/ohj1/paate/paate1  (microclass computer, Linux-muoto)
c:\kurssit\ohj1\paate\paate1                      (own Windows-machine, Windows muoto)
/c/kurssit/ohj1/paate/paate1                      (own Windows-machine, Linux-muoto)
~/kurssit/ohj1/paate/paate1                       (Mac and Linux)

Note that the up arrow button gives you the previous command, so it's easy to add to it. Now, depending on your machine and location, type on the command line:

Own Windows machine (if you have multiple disks, you can use any other drive in place of c):

cd /c
mkdir -p kurssit/ohj1/paate/paate1
cd kurssit/ohj1/paate/paate1

In the microclass:

cd /c/MyTemp
mkdir Anonymous
cd Anonymous
mkdir -p kurssit/ohj1/paate/paate1
cd kurssit/ohj1/paate/paate1

Mac/Linux:

cd ~
mkdir -p kurssit/ohj1/paate/paate1
cd kurssit/ohj1/paate/paate1

So what was done?

  1. changed to the root of the c-directory (Windows) or home directory (Mac, Linux)
  2. created a new directory (mkdir, make directory) my using the complete path (-p or path) and then gave the whole directory path that we wanted to create. This does not need to be created a second time even if you close the command window.
  3. change the directory (cd, change directory)

Do the tasks in this directory. If you have to restart Git Bash, the cd commands to change the directory must be redone.

# dotnet

4. Compiling and running on dotnet

This is a newer and slightly simpler way to compile from the command line. The new way unfortunately hides what is happening in the background. dotnet command is a program that creates a new project, i.e. makes the necessary files according to the given templates (e.g. ConsoleMain or FysiikkaPeli). It then compiles and also runs the compiled program if it is error-free.

First, install the necessary project templates. This is done once/machine, although it doesn't hurt to reinstall , it even updates to a newer version if necessary.

dotnet new install Jypeli.Templates

If the upper one does not work or you are on a university machine then use

dotnet new --install Jypeli.Templates

Change to the directory where you want the project (see 3. Hakemiston luominen ja sinne vaihtaminen).

Create a HelloWorld project

dotnet new ConsoleMain -n HelloWorld

Change to the directory of the created project

cd HelloWorld

See what files were created:

ls -la     

The file can be edited by typing e.g. (assuming that Visual Studio Code is installed):

code HelloWorld.cs

If no editor installation has been made

  • Windows: notepad HelloWorld.cs
  • Mac: open -e HelloWorld.cs
  • Linux: nano HelloWorld.cs

Edit the code (i.e. open the file HelloWorld.cs) in the editor from the file HelloWorld.cs by inserting the code inside the Main function. (i.e. the area between the braces {} there) for example

    System.Console.WriteLine("Hello World!");

Indent the line so that the System word starts 4 spaces inside the braces. Example code:

public class HelloWorld
{
    public static void Main()
    {
        System.Console.WriteLine("Hello World!");
    }
}

Compile and run a program by typing from the command line:

dotnet run

Note that the dotnet run command must be issued from the same folder where is located as the .csproj ending file (in the example, the file is HelloWorld.csproj).

Now the file HelloWorld. cs contains the first source code of the program. Source code is plain text that can be edited with any text editing program. Source code is always written according to the rules of a programming language. In this course, the source code of our program follows the rules of a programming language called C#(C-sharp).

However, the source code is not understood by computers, but must first be compiled into a form that the computer can execute. This is done by a compiler, a separate program whose job is to produce a program that runs from the source code.

So in the .NET environment, the compiling and running is done by that dotnet compiler. For example, in C, is usually compiled with one command and run with another command to produce the compiled program.

These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.