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.
- do the exercises at least once, using only TIM compiling from the command line
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!
Note that Agora's microclass machines can only run programs made under the directories:
c:\MyTemp
u:\Config
N:
-network storage of the university network contains commands for compiling progams and the use of the text editor, among other things. Check that the N:
-disk is mounted for example by typing in PowerShell (you can copy-paste from below)
dir n:
If you get an error message dir : Cannot find drive. A drive with the name 'n' does not exist.
, click on the Yhdista-N
icon on your desktop. If there is no icon, you can mount the disk from the command line:
net use N: \\fileservices.ad.jyu.fi\customershare-multi$\it-ohjelmointi
- For more information on the command line - open this in one of your browser tabs and see here for help if needed
On your own machine you can run similar commands (npp, csm, csk, etc) even in the directory of C:, and add that folder to the so called PATH
search path so the commands are available from anywhere.
Before mentioned works in a microclassroom and on your own computer if it is on the university network (which can be done from home via VPN). But when used from a non-micro class machine, net use
asks for username and password and it also needs a domain
username:
USERNAME@ad.jyu.fi
For example, if the npp
command doesn't work, the place where is typed (i.e. n:\bin
in microclassroom or at home where you've created it) should be added to the search path:
cmd:
path=n:\bin;%path%
PowerShell:
$env:Path += ";n:\bin"
Git Bash:
export PATH="/n/bin:$PATH"
You'll notice that in Git Bash you always have to type npp.bat
when the instruction is npp
.
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?
- changed to the root of the c-directory (Windows) or home directory (Mac, Linux)
- created a new directory (
mkdir
, make directory) my using the complete path (-p
orpath
) 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. - 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.
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.