about
Bits Hello C#
11/17/2023
0
Bits Repo Code Bits Repo Docs

Bits: Hello C#

code, output, and build for C# on Windows, macOS, and Linux

These pages support comparison of fragments of code in several different languages, much as you might compare a sentence of English with one in Spanish to help you learn Spanish or English.

Synopsis:

This page demonstrates use of Visual Studio Code to build and execute a simple C# "Hello World" program, showing its build process and launch file. The purpose is to show how to get started quickly.
  • Companion pages for C++, Rust, Python, and JavaScript are accessible from links in the left panel.
  • Navigation through example levels for the same language is effected with the "Next" and "Prev" buttons in the menus or by using the "N" and "P" keys.
  • The links in the left panel point to major sections of this page and all of the Bits pages for C#.
Note:
Most page links in this site refer to specific points in a target page, usually the top. For all of the code bits here, links return to the last scroll position. This supports quickly moving between similar code for each of the languages covered. That maintains context for comparisons.

Source Code

Code below is a fragment taken from Bits Repository. You can download the repo containing code for all Bits by opening the "Code" dropdown in that page.
// See https://aka.ms/new-console-template for more information
using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello C# World");
    }
  }
}          

Output

This is output from the C# source code above, running in Visual Studio Code. The output is retrieved in an embedded terminal window. VS Code runs on Windows, Linux, and macOS.
C:\github\JimFawcett\Bits\CSharp\Cs_Hello
> dotnet run

  Hello C# World

C:\github\JimFawcett\Bits\CSharp\Cs_Hello

Build

To build a project with dotnet you use two commands, dotnet build to build and dotnet run to build and run the project. dotnet is a command line application provided as part of a Visual Studio (Community Edition) installation.
C:\github\JimFawcett\Bits\CSharp\Cs_Hello
> dotnet build
MSBuild version 17.4.1+fedecea9d for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Cs_Hello -> C:\github\JimFawcett\Bits\CSharp\Cs_Hell
  o\bin\Debug\net7.0\Cs_Hello.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.75
C:\github\JimFawcett\Bits\CSharp\Cs_Hello
>          

Create Project

The terminal session below shows how to create a project for C# with dotnet infrastructure. See tooling information here: Bits_Tooling.
C:\github\JimFawcett\test
> dotnet new console -n CSharp_Hello
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring C:\github\JimFawcett\test\CSharp_Hello\CSharp_Hello.csproj:
  Determining projects to restore...
  Restored C:\github\JimFawcett\test\CSharp_Hello\CSharp_Hello.csproj (in 50 ms).
Restore succeeded.


C:\github\JimFawcett\test
> cd CSharp_Hello
C:\github\JimFawcett\test\CSharp_Hello
> dir

    Directory: C:\github\JimFawcett\test\CSharp_Hello

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/24/2023   9:41 AM                obj
-a----         2/24/2023   9:41 AM            249 CSharp_Hello.csproj
-a----         2/24/2023   9:41 AM            105 Program.cs


C:\github\JimFawcett\test\CSharp_Hello
>

2.0 VS Code View

The code for this demo is available in github.com/JimFawcett/Bits. If you click on the Code dropdown you can clone the repository of all code for these demos to your local drive. Then, it is easy to bring up any example, in any of the languages, in VS Code. Here, we do that for Cpp\Cpp_Hello. Figure 1. VS Code IDE - C# Hello Figure 2. Debugging C# Figure 2. C# Hello Launch.JSON Figure 3. C# Plugins

3.0 References

ReferenceDescription
C# Tutorial - Microsoft Video and Presentations of simple examples
Interactive Tutorials - W3Schools Short examples with remote execution
Bits Tooling Tool chains, VS Code