Introduction:
C# is a powerful, modern programming language developed by Microsoft, widely used for building web applications, desktop software, cloud services, and more. In this guide, we will walk through creating both a console application and a web application using .NET Core and Visual Studio Code.
By the end of this tutorial, you will:
- Set up your development environment with .NET Core and VS Code
- Begin the setup of a web application using ASP.NET Core
- Create a simple C# console application using AI generated code
Prerequisites:
Install .NET SDK from here or through VS code using this extension
Download and install Visual Studio Code from code.visualstudio.com
Let's dive in!In your local machine, create a folder where we will make these apps. Open the terminal, VS code terminal or powershell and navigate to this folder
In the command line, enter:
dotnet --version
You should get a response similar to below. If get an error, you need to verify that .NET is properly installed and set up on your local machine
Creating and Running the Web App
- Open the folder in VS code. Navigate to the terminal. Enter:
dotnet new web -n MyWebApp
-
Breakdown:
- dotnet new → Creates a new .NET project.
- web → Uses the ASP.NET Core minimal web app template.
- n MyWebApp → Names the project MyWebApp and creates a folder for it.
Open
Program.cs
and look at the code inside.In the terminal navigate to the MyWebApp folder, enter
dontnet run
Look in the terminal. Hold control (or command for Mac) and click where it gives the localhost link. This will open the app in your web browser
Go back to VS code and press Control + c to stop the server. Look at the code in Program.cs. Notice this part in the code:
app.MapGet("/", () => "Hello World!");
This controls what is displayed. Try changing the displayed message. Example:
app.MapGet("/", () => "I'm learning C#!");
- In the terminal, enter
dotnet build
dotnet run
-
Explanation:
-
dotnet build
: Compiles the C# code and checks for errors. Then generates an optimized binary (DLL file) inside the bin/ folder. -
dotnet run
: Runs the compiled application on a web server that listens for requests.
-
Creating and Running the Console App
- Enter
control + c
in the terminal to stop the web app server. - Navigate out of the web app folder in the terminal using the command
cd ..
- To set up the files needed for a console app, enter the comand:
dotnet new console -n MyConsoleApp
This will create a folder called My console app with the necessary files to run a console app. Open the folder and navigate to
Program.cs
In the terminal, navigate to the console app folder then enter
dotnet run
- This will build and run the app. Note the output in the terminal:
As you can see, console apps primarily run in the terminal.
Generating and running sample code in the console
Open a generative AI of your choice. Enter a prompt similar to this:
create a snake game that runs in c sharp console
Replace the contents of
Program.cs
with the generated code and save the fileIn the terminal enter :
dotnet build
dotnet run
- You can now use the app in the console
Summary
This article introduces C# development with .NET Core using Visual Studio Code. We explore the basics of creating and running both console and web applications using .NET.
Key Takeaways:
✅ Setting up the environment – using .NET SDK and VS Code
✅ Setting up a C# web application – Creating a minimal ASP.NET web app
✅ Setting up a C# console app – Generating and running a Snake game that runs in the terminal
✅ Modifying and running the app – Editing Program.cs and using dotnet build
& dotnet run
This hands-on approach demonstrates how C# and .NET Core can be used to create both interactive console and web applications, making it a powerful tool for development. 🚀
Top comments (0)