SyntaxStudy
Sign Up
C# Creating and Running a .NET 8 Console Project
C# Beginner 1 min read

Creating and Running a .NET 8 Console Project

The .NET CLI is the primary tool for creating, building, running, and publishing C# projects. Running `dotnet new console` scaffolds a complete console application with a project file (.csproj) and a Program.cs entry point. The project file uses MSBuild XML to declare the target framework, nullable reference type settings, and NuGet package references. Running `dotnet run` compiles and executes the project in one step, making the inner development loop fast. For production use `dotnet publish` to produce a self-contained or framework-dependent deployment artefact. Self-contained builds bundle the runtime so the target machine needs no .NET installation. NuGet is the package manager for .NET. You can add packages with `dotnet add package ` and they are recorded in the .csproj file. The package restore step downloads dependencies automatically before the first build, so checking the .csproj into source control is all that's needed.
Example
// Sample .csproj for a .NET 8 console application
/*
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>
*/

// Program.cs — top-level statements entry point
using System.Text.Json;

// Implicit usings (enabled in csproj) auto-import common namespaces:
// System, System.Collections.Generic, System.IO, System.Linq, etc.

var person = new { Name = "Alice", Age = 30 };

// Serialize to JSON using System.Text.Json (built-in, no NuGet needed)
string json = JsonSerializer.Serialize(person, new JsonSerializerOptions
{
    WriteIndented = true
});

Console.WriteLine(json);

// Deserialize back to a dynamic object
var restored = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
if (restored is not null)
{
    Console.WriteLine($"Name: {restored["Name"].GetString()}");
    Console.WriteLine($"Age:  {restored["Age"].GetInt32()}");
}