🎯 Key Takeaways: Build a modern console application using C# 13 features. Includes primary constructors, params collections, and more.
Console apps aren’t dead. C# 13 makes them better than ever.
🚀 Create Project
# Create console app with .NET 9 (C# 13 support) dotnet new console -n ModernConsole cd ModernConsole # Enable C# 13 features dotnet add package Microsoft.Net.Compilers.Toolset --version 4.9.0
💡 Program.cs
using ModernConsole.Models;
using ModernConsole.Services;
// ✅ C# 13: Top-level statements with primary constructor
var app = new ConsoleApp(args);
await app.RunAsync();
// ✅ C# 13: Primary constructor on record
public record AppSettings(
string Environment,
string ConnectionString,
int MaxRetries,
bool IsDebugMode);
// ✅ C# 13: Primary constructor on class
public class ConsoleApp(string[] args)
{
private readonly AppSettings _settings = LoadSettings();
private readonly ILogger _logger = new ConsoleLogger();
public async Task RunAsync()
{
_logger.LogInfo("Console app starting with {Count} arguments", args.Length);
// ✅ C# 13: Params collections
if (args.Length > 0 && args[0] == "--help")
{
ShowHelp();
return;
}
// ✅ Main application logic
await ProcessAsync();
_logger.LogInfo("Console app completed");
}
private static AppSettings LoadSettings()
{
// ✅ C# 13: Field keyword in property
return new AppSettings(
Environment: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development",
ConnectionString: Environment.GetEnvironmentVariable("CONNECTION_STRING") ?? "Default",
MaxRetries: int.TryParse(Environment.GetEnvironmentVariable("MAX_RETRIES"), out var retries) ? retries : 3,
IsDebugMode: bool.TryParse(Environment.GetEnvironmentVariable("DEBUG_MODE"), out var debug) ? debug : false
);
}
private async Task ProcessAsync()
{
_logger.LogInfo("Processing in {Environment} environment", _settings.Environment);
// ✅ Simulate work with retry
for (int i = 0; i < _settings.MaxRetries; i++)
{
try
{
await DoWorkAsync();
break;
}
catch (Exception ex)
{
_logger.LogError("Attempt {Attempt} failed: {Error}", i + 1, ex.Message);
if (i == _settings.MaxRetries - 1)
{
_logger.LogError("All retries exhausted");
throw;
}
await Task.Delay(1000 * (i + 1));
}
}
}
private async Task DoWorkAsync()
{
// Simulate work
await Task.Delay(1000);
Console.WriteLine("Work completed successfully!");
}
private void ShowHelp()
{
// ✅ C# 13: Collection expressions
string[] helpLines = [
"Modern Console App Help",
"=======================",
"",
"Usage: dotnet run [options]",
"",
"Options:",
" --help Show this help message",
",
"Environment Variables:",
" ASPNETCORE_ENVIRONMENT Development | Staging | Production",
" CONNECTION_STRING Database connection string",
" MAX_RETRIES Maximum retry attempts (default: 3)",
" DEBUG_MODE true | false (default: false)"
];
foreach (var line in helpLines)
{
Console.WriteLine(line);
}
}
}
// ✅ Simple logger
public class ConsoleLogger : ILogger
{
public void LogInfo(string message, params object[] args)
{
Console.WriteLine($"[INFO] {string.Format(message, args)}");
}
public void LogError(string message, params object[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[ERROR] {string.Format(message, args)}");
Console.ResetColor();
}
}
public interface ILogger
{
void LogInfo(string message, params object[] args);
void LogError(string message, params object[] args);
}



