Creating a Console Application
Go to File β New β Project. Search for Console App and select the C# version (not the VB.NET one). Click Next, give the project the name HelloWorld, choose a folder, and click Create. Visual Studio generates a single file called Program.cs.
// .NET 10 Console App β no explicit Main method required
Console.WriteLine("Hello, World!");Anatomy of a C# Program
In classic C# a program required a namespace, a class, and a static void Main() entry point. C# 9+ introduced top-level statements which remove this ceremony. Behind the scenes the compiler still generates a Main methodβit is just hidden from you.
// Classic style (still valid in C# 14)
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Classic style");
}
}
}
// Modern top-level style (C# 9+, recommended)
Console.WriteLine("Modern style");Reading User Input
Console.ReadLine() reads a line of text the user types and returns it as a string. int.Parse() converts a string to an integer. If the string is not a valid number, int.TryParse() is safer because it returns false instead of throwing an exception.
Console.Write("Enter your name: ");
string name = Console.ReadLine() ?? "stranger";
Console.Write("Enter your birth year: ");
if (int.TryParse(Console.ReadLine(), out int year))
{
int age = DateTime.Now.Year - year;
Console.WriteLine($"Hello {name}, you are approximately {age} years old!");
}
else
{
Console.WriteLine("That doesn't look like a valid year.");
}Building & Running
Press Ctrl+Shift+B to build. Press Ctrl+F5 to run without debugging (the console window stays open). Press F5 to run with debugging. The build output appears in the Output window; errors appear in the Error List.