Hello World in C#

Before you dive into ASP.NET Core, try creating and running a simple C# application.

You can do this all from the command line. First, open up the Terminal (or PowerShell on Windows). Navigate to the location you want to store your projects, such as your Documents directory:

  1. cd Documents

Use the dotnet command to create a new project:

  1. dotnet new console -o CsharpHelloWorld
  2. cd CsharpHelloWorld

This creates a basic C# program that writes some text to the screen. The program is comprised of two files: a project file (with a .csproj extension) and a C# code file (with a .cs extension). If you open the former in a text or code editor, you’ll see this:

CsharpHelloWorld.csproj

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <OutputType>Exe</OutputType>
  4. <TargetFramework>netcoreapp2.0</TargetFramework>
  5. </PropertyGroup>
  6. </Project>

The project file is XML-based and defines some metadata about the project. Later, when you reference other packages, those will be listed here (similar to a package.json file for npm). You won’t have to edit this file by hand often.

Program.cs

  1. using System;
  2. namespace CsharpHelloWorld
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine("Hello World!");
  9. }
  10. }
  11. }

static void Main is the entry point method of a C# program, and by convention it’s placed in a class (a type of code structure or module) called Program. The using statement at the top imports the built-in System classes from .NET and makes them available to the code in the class.

From inside the project directory, use dotnet run to run the program. You’ll see the output written to the console after the code compiles:

  1. dotnet run
  2. Hello World!

That’s all it takes to scaffold and run a .NET program! Next, you’ll do the same thing for an ASP.NET Core application.