C# Basics
C# Environment Setup
Windows:
- Visual Studio Code: .Net 8 (tested on Windows 10)
MacOS:
- Visual Studio Code: .Net 8 (tested on Sequoia 15.3)
Linux:
- Visual Studio Code: .Net 8 (tested on Unbuntu 24.04)
Install Visual Studio Code and .NET 8 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.
- 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):
- Windows: Java runtime
- Mac OS: Java runtime + graphviz
- Usage: Alt+D (Windows)/Option+D (Mac OS) to enable the preview function
Markdown All in One via Extension
- Usage: 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 .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.
$ dotnet new globaljson --sdk-version 8.0.302 --roll-forward latestFeature
// Check for update
$ dotnet sdk check
More about globaljson can be found here.
Check Your Installed .Net SDK
In Program.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>net8.0;net47</TargetFrameworks>
First Console Program
// Use top-level statements
$ dotnet new console
// Skip top-level statements and include Main()
$ dotnet new console --use-program-main
// Create a new console project with a specific project name
$ dotnet new console --name MyProject
or call .NET New Project 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>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
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;
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
- 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
Selected Theory
Naming Convention Doc
Pascal Case
Use pascal casing (“PascalCasing”) when naming a class, record, or struct. When naming public members of types, such as fields, properties, events, methods, and local functions, use pascal casing. When naming an interface, use pascal casing in addition to prefixing the name with an I. This clearly indicates to consumers that it’s an interface.
Camel Case
Use camel casing (“camelCasing”) when naming private or internal fields, and prefix them with _.
Comments, an Example
// This is a single line comment
/*
This is a multi-line comment
and continues until the end
of comment symbol is reached
*/
/*
The following codes show how I often add comments to my programs.
This is a my application namespace, called My Business.
*/
namespace CRC_CSD_01;
/*
The class of my main program
*/
class Program
{
/*
Main: The class of my main program, where the application begins.
Input:
args: input parameters
Output:
none
*/
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
Potential Errors
- Explorer in VSCode is missing. Ctrl+Shift+P to enable the vscode Command Palette and run the command “View: Reset View Locations”.