DEV Community

Cover image for Getting Started with C# and .NET Core: Build a Web and Console App Using VS Code
John Ogbonna
John Ogbonna

Posted on

Getting Started with C# and .NET Core: Build a Web and Console App Using VS Code

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
Enter fullscreen mode Exit fullscreen mode

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
properly installed

Creating and Running the Web App

  • Open the folder in VS code. Navigate to the terminal. Enter:
dotnet new web -n MyWebApp
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
    web browser

  • In the browser you will see this:
    In the browser you will see this

  • 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#!");
Enter fullscreen mode Exit fullscreen mode
  • In the terminal, enter
dotnet build
dotnet run
Enter fullscreen mode Exit fullscreen mode
  • 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.
  • Open the new localhost

    Open the new localhost

  • You should see the new message

    You should see the new message

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
Enter fullscreen mode Exit fullscreen mode
  • 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
    Program.cs

  • In the terminal, navigate to the console app folder then enter

dotnet run
Enter fullscreen mode Exit fullscreen mode
  • This will build and run the app. Note the output in the terminal: 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 file

  • In the terminal enter :

dotnet build
dotnet run
Enter fullscreen mode Exit fullscreen mode
  • You can now use the app in the console 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. 🚀

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay