Introduction
Visual Studio Code (VS Code) is a lightweight yet powerful code editor that supports C# development with ease. Whether you're building a simple console application or a basic web app, correctly setting up your development environment is essential. This guide will walk you through setting up VS Code to create a simple C# console app and a minimal web app (a number guessing game) using ASP.NET Core.
Prerequisites
Before you start, ensure you have the following installed:
Once these are installed, open VS Code and install the C# extension if you haven’t already. This extension provides IntelliSense, debugging, and other essential tools for C# development.
Setting Up a C# Console Application
- Create a New Project Open a terminal in VS Code and run:
dotnet new console -o MyConsoleApp
cd MyConsoleApp
code .
This creates a new console application and opens it in VS Code.
-
Write Code
Open
Program.cs
and modify theMain
method to print a message:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, VS Code with C# is fun :)!");
}
}
- Run the Application In the terminal, execute:
dotnet run
You should see Hello, VS Code with C# is fun :)!
printed in the console.
Setting Up a Simple Web App with ASP.NET Core
- Create a Web App Run the following command to create a new web project:
dotnet new web -o MyWebApp
cd MyWebApp
code .
-
Modify the Web App
Open
Program.cs
and update it as follows:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace NumberGuessGame
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
int secretNumber = new Random().Next(1, 101);
context.Session.SetInt32("SecretNumber", secretNumber);
await context.Response.WriteAsync($"");
await context.Response.WriteAsync("Number Guessing Game");
await context.Response.WriteAsync("");
await context.Response.WriteAsync("Enter your guess: ");
await context.Response.WriteAsync("");
await context.Response.WriteAsync("");
await context.Response.WriteAsync("");
});
endpoints.MapPost("/guess", async context =>
{
var form = await context.Request.ReadFormAsync();
int userGuess = int.Parse(form["guess"]);
int secretNumber = context.Session.GetInt32("SecretNumber") ?? new Random().Next(1, 101);
string message;
if (userGuess < secretNumber)
message = "Too low! Try again.";
else if (userGuess > secretNumber)
message = "Too high! Try again.";
else
{
message = "Congratulations! You guessed the right number!";
secretNumber = new Random().Next(1, 101); // Reset number after a correct guess
context.Session.SetInt32("SecretNumber", secretNumber);
}
await context.Response.WriteAsync($"");
await context.Response.WriteAsync($"{message}");
await context.Response.WriteAsync("Try Again");
await context.Response.WriteAsync("");
});
});
}
}
}
Enter fullscreen mode
Exit fullscreen mode
Run the Web App
Execute:
dotnet run
Enter fullscreen mode
Exit fullscreen mode
The app will start on http://localhost:5230. Open a browser and visit the URL to see your message.Conclusion
Setting up VS Code for C# development is straightforward. By installing the .NET SDK and the C# extension, you can quickly create and run both console and web applications. This setup provides a lightweight yet practical development environment for C# programmers.