Your first C# program
Install the tools, create a console app, and understand every line of “Hello, World!”.
Install the tools
Install Visual Studio Community (free) and tick the “.NET desktop development” workload. It includes the .NET SDK. Alternatively install the .NET SDK plus VS Code with the C# Dev Kit extension. Open a terminal and run dotnet --version. If you see a number like9.0.100, the SDK is installed.
Create and run a console app
dotnet new console -n HelloAppcd HelloAppdotnet run Output
Hello, World!
Every line explained
The short version: top-level statements
Program.cs. The compiler adds the namespace, class and Main method for you behind the scenes. Both versions do exactly the same thing.
// Program.cs (top-level statements)Console.WriteLine("Hello, World!"); Output
Hello, World!
Write vs WriteLine
Console.Write("Hello, ");Console.Write("Ravi");Console.WriteLine("!"); // ends the lineConsole.WriteLine("Next line"); Output
Hello, Ravi! Next line
Rules C# is strict about
C# is case-sensitive: Consoleworks,consoledoes not.Every statement ends with a semicolon ;.Text (strings) goes in double quotes: "Hello".Every {needs a matching}.
Comments
// A single-line comment /* A comment that spans several lines */ /// <summary>XML documentation comment, shown in IntelliSense.</summary> Practice
Show the solution
Console.WriteLine("Raviselva");Console.WriteLine("Salem");Console.WriteLine("I am learning C#"); Output
Raviselva Salem I am learning C#
Remember
Create and run a console app with dotnet new consoleanddotnet run(or F5 in Visual Studio).Mainis the entry point; top-level statements are a shorter way to write it.Console.WriteLineprints and starts a new line;Console.Writedoes not.C# is case-sensitive, statements end with ;, blocks use{ }.