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 commandsdotnet 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 projectdotnet test
run UTdotnet 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:
- create project. In
/src/GradeBook
create c# console project viadotnet new console
- Run console project, in folder where there is
.csproj
, rundotnet run
.- Or, if in different directory, run
dotnet run --project <.csproj directory>
- Or, if in different directory, run
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
dotnet restore
: get all dependenciesdotnet 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:/bin
for binary files/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 viadotnet
).- e.g.
dotnet bin/Debug/net5.0/GradeBook.dll
can run the bin file.
- e.g.
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:
- Add breakpoint
- Start Debugging (F5), code will stop at breakpoint
- So we can inspect local variables
- Then we can Continue
We can set static args input in .vscode -> launch.js -> args: [...]