Your First Program
Here are the steps needed to build your own program:
- Create a new folder for the project, and move into this in the terminal
- Use
dotnet new console
to create the initial project files - Add the SplashKit package we will be using
dotnet add package splashkit
- Write the code in Program.cs
- Build and run using
dotnet run
Let’s go through these steps to build a simple terminal-based program to output the text: “Hello, World”.
Make sure you are set up
We need somewhere to put our project’s code. For this, we will create a folder in our Documents/Code folder. You should have created this folder as part of the tour in the Computer Use chapter, but we will repeat it here just in case you missed that step.
You can choose anywhere to store your code, if you want to save your code elsewhere then make sure to adjust paths as needed. Open a terminal, and use the following commands to create the Documents/Code folder:
Create the project folder
Now you can create a folder for your project. Make sure you are in your Documents/Code folder, and run the following commands:
Create dotnet project files
We will be using C# initially, for these programs we need to setup project files that the language uses to know how to build your program.
To create the initial project files for this C# program, copy and paste the following commands into your Terminal window:
This creates the project, and add the splashkit library we will be using.
If you run ls -lha
in the terminal after the commands above, it should look similar to this:
Now you are ready to start coding your first program!
Write your code
From the terminal window, we can open the current folder in Visual Studio Code using the following command:
You will notice that shortly after opening the project, a bin folder and a .sln file will be added automatically:
Open Program.cs
file (shown above in Green box) by double-clicking the file in the Explorer tab (shown above in Orange box):
Copy the following code into your Program.cs file (replacing the existing code):
After the last line, you can add another WriteLine("...")
bit of code with your own custom message!
Build and run your code
Let’s turn your code into a program! Like magic!
Use the following command to build and run the program:
Here is what the result will look like:
Next, we are going to create a Graphical Hello World program!