C# Basics

C# Environment Setup

Windows/MacOS/Linux: Visual Studio Code + .Net 10

Install Visual Studio Code and .NET 10 SDKs

Install Visual Studio Code Extension

  • C# Dev Kit
    • Using C# Dev Kit requires you to sign in with a Microsoft account that has an active Visual Studio subscription. Visual Studio Community, for example. You can sign in with your own account or ustp account.
  • C#
  • Markdown All in One: Extension for Markdown documents.
  • CSharp to PlantUML: Extension for converting Calss relationship in C# to PUML format.
  • PlantUML: Extension for visualizing PUML format in an image.

Additional Settings (Optional)

  • Turn on Word Wrap in the Setting if preferred.

CSharp to PlantUML via Extension of VSCode

  • Usage: Ctrl+Shift+P to enable the vscode Command Palette and run the command “csharp2plantuml.classDiagram”.

PlantUML via Extension of VSCode

  • PlantUML is an Extension for viewing *.puml files.
  • Prerequisite maybe needed (check the documentation of the extension):
  • Usage: Alt+D (Windows)/Option+D (Mac OS) to enable the preview function

Markdown All in One via Extension

  • Usage: Ctrl + Shift + V or Ctrl K+V to preview the file

Check Your Installed .Net SDK

// Display help
$ dotnet -h|--help
// check the installed .Net in your system
$ dotnet --info 
// check the current .Net Version used for command line
$ dotnet --version
// check all installed SDKs
$ dotnet --list-sdks
// If you want to change your .Net Version, add the globaljson file
// The global.json file allows you to define which .NET SDK version 
// is used when you run .NET CLI commands.
// Uses the highest installed feature band and patch level that matches 
// the requested major and minor with a feature band and patch level that is 
// greater than or equal to the specified value. If not found, fails.
// --roll-forward latestFeature: Configures the SDK to use the highest installed feature band and patch level for the specified major/minor version.
$ dotnet new globaljson --sdk-version 8.0.302 --roll-forward latestFeature
# $ dotnet new globaljson --force --sdk-version 8.0.302 --roll-forward latestFeature
// 8.0.302 follows a specific Major.Minor.Patch structure
// 8 (Major): Represents the major .NET release (e.g., .NET 8).
// 0 (Minor): Aligns with the minor version of the .NET runtime.
// 100 (SDK Patch): This 3-digit number is split into: 1 (Feature Band) + 00 (Patch Level)
// Check for update
$ dotnet sdk check

More about globaljson can be found here.

First Console Program

// Create a new console project with a specific project name
$ dotnet new console --name MyProject
// Use top-level statements
$ dotnet new console 
// Skip top-level statements and include Main()
$ dotnet new console --use-program-main 

or call .NET New Project (Ctrl+Shift+P) in the VSCode Command Palette

In Program.cs Doc

Use top-level statements

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Skip top-level statements and include Main

namespace CRC_CSD_01;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

In CRC_CSD-00.csproj Doc

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

In CRC_CSD-00.csproj

// You build your project against APIs defined in a target framework moniker (TFM). You specify the target framework in the project file.
  <TargetFramework>net9.0</TargetFramework>
// or multiple target frameworks. Note that the element name is now plural.
  <TargetFrameworks>net10.0;net8.0;net47</TargetFrameworks>

How to Run the Program

// Build the program
$ dotnet build
// Run the program (Including Build, so you can skip the above command) 
$ dotnet run

Namespace in C#

using System;         // System namespace (defined by C#)
                      // The "using" Directive
// or
// using static System.Console;  // Console is a static system class
/*
A static class is basically the same as a non-static class,
but there is one difference: a static class cannot be instantiated.
*/

namespace CRC_CSD_01; // Application namespace (defined by the programmer)

class Program
{
    // static: shared method of all instances by the class
    // void: return "nothing" in the method
    // string[] args: parameters passed to the main function.
    // The parameters can be taken when lauching the application.
    static void Main(string[] args)  // Where the application begins
    {
        Console.WriteLine("Hello, World!");   // Console is a system class
    }
}

Console Program with Accompanying Parameters

using System;

namespace CRC_CSD_01; // File scoped namespaces

class Program
{
    // string[] args: parameters passed to the main function.
    static void Main(string[] args) // Where the application begins
    {
        Console.WriteLine("The length of the arguments: " + args.Length);
        for( int i = 0; i < args.Length; i++ ){
            Console.WriteLine(args[i]);
        }
    }
}

Introduction to Markdown language

Example for a README.md file in your Project

  • Best README Template

  • About The Project
    • Built With
  • Getting Started: How to install and set up your app.
    • Prerequisites
    • Installation
  • Usage: Show useful examples of how a project can be used.
  • Roadmap: What have been implemented and what are the planed features.
  • Contributing: Encourage people to work on your project.
  • License: Your project license
  • Contact
  • Acknowledgments

Potential Errors

  • Explorer in VSCode is missing. Ctrl+Shift+P to enable the vscode Command Palette and run the command “View: Reset View Locations”.

External Resources