This page is a work in progress.You can help improve it. →

Missing Document Title

theme: Next, 1

Creating and running a C# program


Projects

  • Before we can write code in C# we must create a project.

  • Each project will be in its own directory.

  • We should create these projects in an organized parent folder such as sdg in our home directory


.NET provides a tool for creating new projects!


[fit] Introducing dotnet new

  • This command creates new projects from a template
  • Includes starter code for us!
  • We'll be using it A LOT

[fit] Let's make our first C# project together!

inline


[fit] Being in the right place at the right time

  • In your shell change to the directory where you are going to keep your projects.
  • Good options:
    • sdg directory in home folder
    • sdg directory on your desktop
    • sdg directory in your Documents folder
    • Or name the directory code, school or dev; but do keep all your projects together in a folder.


Example:

cd sdg

[fit] Create our app!

  • Use the dotnet tool to create our application


dotnet new sdg-console -o OurDotnetApp

[fit] What does this do?

# command action template output project-name
# | | | | |
# v v v v v
dotnet new sdg-console -o OurDotnetApp

[fit] Where is our project?

We need to change directory into the newly created directory



cd OurDotnetApp

[fit] Open our project in our editor!

Now we can open our project in our editor!



code .

[fit] Let's take a look around

inline


[fit] dotnet new creates initial files for us

Let's see what files appear in our folder.

├── Program.cs
└── OurDotnetApp.csproj

We may also see an obj folder but we will ignore that for a moment.

^ There are two files in our folder. The first, OurDotnetApp.csproj is a file that dotnet wrote for us. It contains details about the project itself such as which version of dotnet our program needs. For the most part, we are not going to modify this file


Run the code

The template Program.cs prints out the phrase Welcome to C#.

To get dotnet to run our program and see if the phrase appears on our screen we will use the dotnet run command.


dotnet run

Output

We should see the following output on the screen if everything is correct:


Welcome to C#

Now as we change our code and add more functionality we can return to our terminal/Powershell and run dotnet run again to see our new code in action.


Run our code automatically

We can also use the dotnet command to keep track of our code and run it every time we save our code!


dotnet watch run

dotnet watch run
watch : Started
Welcome to C#
watch : Exited
watch : Waiting for a file to change before restarting dotnet...

^ If you find yourself in a cycle of:

^ - Change code

^ - Run dotnet run

^ - See output

^ - Repeat

© 2017 - 2022; Built with ♥ in St. Petersburg, Florida.