1
1_introduction_csharp_and_dotnet
Jason Zhu edited this page 2021-02-23 13:57:37 +11:00

Module 1. Introducting C# and .NET

Prerequisit:

  • Download .NET Core SDK (>2.2)
  • Download VSCode

Understanding .NET and .NET Core

2 .NET Frameworks:

  • .NET Framework (Windows Only)
  • .NET Core: newer, open-source and cross-platform. Good for grassfield projects

Understanding .NET Runtime

Similar as Java, Ruby, Python, any high level programming languages. They need software as runtime environment.

.NET = framework to tell OS how to compile/run C# code, has 2 Parts:

  • CLR (Common Language Runtime) provide space for C# program to run in real time (e.g. send instruction to Intel, ARM or AMD). Usually called "Runtime"
  • FCL (Framework Class Library) are common libraries for your C# programs to handle file, network, etc. Usually called "Library"

SDK (Software Development Kit) include .NET runtime, FCL and additional development libraries.

Using the .NET Command Line

  • dotnet --info shows information of dotnet env e.g. Runtime Environment (OS), version of SDK.
  • dotnet -h help documentation, shows SDK commands
  • dotnet build bulid a .NET project (compile). A project = a collection of src file that group together to work. So, before create a new file, need a new project
  • dotnet test run UT
  • dotnet new create new .NET project.

Creating the First C# Project

dotnet new require template (short) name to know what kind of project it should build

e.g. create a directory gradebook, within it create a src for source file project, and create a test for .NET test project

.
├── src
│   └── GradeBook
└── test

Steps:

  1. create project. In /src/GradeBook create c# console project via dotnet new console
  2. Run console project, in folder where there is .csproj, run dotnet run.
    1. Or, if in different directory, run dotnet run --project <.csproj directory>

Running and Building Your Project

Nuget & dotnet restore:

  • In .NET world, third party open-sourcelibraries & extensions are managed by NuGet
  • When certain feature is required in the project, add it in .csproj file.
  • dotnet restore read through .csproj to locate dependencies
  1. dotnet restore: get all dependencies
  2. dotnet build: rebuild project. Compile src code into a single binary executable (i.e. dll, e.g. /bin/Debug/net5.0/GradeBook.dll). build process create 2 directories:
    1. /bin for binary files
    2. /obj for temporary files generated during restore/build process

Note:

  • dll file in .NET Core is Assembly, not Dynamic Link Library
  • The only important files in repo is src file and .csproj
  • Execute .dll directly in PowerShell will fail as it has to be run in .NET Core runtime (launched via dotnet).
    • e.g. dotnet bin/Debug/net5.0/GradeBook.dll can run the bin file.

Saying Hello with C#

Method Main is the entrypoint of C# program, we can parse args to it using Zero-based Indexing.

e.g.

        static void Main(string[] args)
        {
            Console.WriteLine("Hello " + args[0]);
        }

String Interpretation can be used to substitude values into string.

        static void Main(string[] args)
        {
            Console.WriteLine($"Hello, {args[0]} !");
        }
    }

dotnet run know assembly is out of date and will rebuild the project and run it.

dotnet run GOOD
dotnet run -- GOOD # tell cli that passing non-parameter argument

Debugging a C# Application

In VSCode:

  1. Add breakpoint
  2. Start Debugging (F5), code will stop at breakpoint
  3. So we can inspect local variables
  4. Then we can Continue

We can set static args input in .vscode -> launch.js -> args: [...]