Chapter 1 - Hello, C#! Welcome, .NET!
January 23, 2025 ยท View on GitHub
Command-Lines
To make it easier to enter commands at the prompt, this page lists all commands as a single line that can be copied and pasted.
Note: Page numbers will be updated once the final print files are made available to me in November 2023.
- Chapter 1 - Hello, C#! Welcome, .NET!
- Page 11 - Managing Visual Studio Code extensions at the command prompt
- Page 15 - Listing and removing versions of .NET
- Page 21 - Understanding top-level programs
- Page 27 - Writing code using Visual Studio Code
- Page 30 - Compiling and running code using the dotnet CLI
- Page 31 - Adding a second project using Visual Studio Code
- Page 37 - Cloning the book solution code repository
- Page 38 - Getting help for the dotnet tool
- Chapter 2 - Speaking C#
- Chapter 4 - Writing, Debugging, and Testing Functions
- Chapter 7 - Packaging and Distributing .NET Types
- Page 379 - Controlling the .NET SDK
- Page 384 - Understanding dotnet commands
- Page 385 - Getting information about .NET and its environment
- Page 386 - Publishing a self-contained app
- Page 388 - Publishing a single-file app
- Page 390 - Enabling assembly-level trimming
- Page 390 - Enabling type-level and member-level trimming
- Page 395 - Publishing a native AOT project
- Chapter 9 - Working with Files, Streams, and Serialization
- Chapter 10 - Working with Data Using Entity Framework Core
- Chapter 11 - Querying and Manipulating Data Using LINQ
- Chapter 12 - Introducing Web Development Using ASP.NET Core
- Chapter 13 - Building Websites Using ASP.NET Core Razor Pages
- Chapter 14 - Building and Consuming Web Services
- Chapter 15 - Building User Interfaces Using Blazor
Chapter 1 - Hello, C#! Welcome, .NET!
Page 11 - Managing Visual Studio Code extensions at the command prompt
code --install-extension ms-dotnettools.csdevkit
Page 15 - Listing and removing versions of .NET
Listing all installed .NET SDKS:
dotnet --list-sdks
Listing all installed .NET runtimes:
dotnet --list-runtimes
Details of all .NET installations:
dotnet --info
Page 21 - Understanding top-level programs
If you are using the dotnet CLI at the command prompt, add a switch to generate a console app project using the legacy Program class with a Main method:
dotnet new console --use-program-main
Page 27 - Writing code using Visual Studio Code
Using the dotnet CLI to create a new solution named Chapter01:
dotnet new sln --name Chapter01
Creating a new Console App project in a folder named HelloCS with a project file named HelloCS.csproj:
dotnet new console --output HelloCS
Adding a named project to the solution file:
dotnet sln add HelloCS
Opening Visual Studio Code in the current folder:
code .
Creating a new Console App project named HelloCS that targets a specified framework version, for example, .NET 6:
dotnet new console -f net6.0 -o HelloCS
Page 30 - Compiling and running code using the dotnet CLI
dotnet run
Page 31 - Adding a second project using Visual Studio Code
Creating a project named AboutMyEnvironment using the legacy Program class with a Main method:
dotnet new console -o AboutMyEnvironment --use-program-main
Page 37 - Cloning the book solution code repository
git clone https://github.com/markjprice/cs12dotnet8.git
Page 38 - Getting help for the dotnet tool
Getting help for a dotnet command like build from the documentation web page:
dotnet help build
Getting help for a dotnet command like build at the command prompt:
dotnet build -?
Getting help for a specified project template, for example, console:
dotnet new console -?
Chapter 2 - Speaking C#
Page 55 - How to output the SDK version
Output the current version of the .NET SDK:
dotnet --version
Page 95 - Exploring more about console apps
Example of a command line with multiple arguments:
dotnet new console -lang "F#" --name "ExploringConsole"
Page 106 - Passing arguments to a console app
Passing four arguments when running your project:
dotnet run firstarg second-arg third:arg "fourth arg"
Setting options using arguments:
dotnet run red yellow 50
Chapter 4 - Writing, Debugging, and Testing Functions
Page 201 - Hot reloading using Visual Studio Code and dotnet watch
Starting a project using Hot Reload:
dotnet watch
Page 205 - Configuring trace listeners
Running a project with its release configuration:
dotnet run --configuration Release
Running a project with its debug configuration:
dotnet run --configuration Debug
Page 207 - Adding packages to a project in Visual Studio Code
Adding the Microsoft.Extensions.Configuration.Binder package:
dotnet add package Microsoft.Extensions.Configuration.Binder
Adding the Microsoft.Extensions.Configuration.Json package:
dotnet add package Microsoft.Extensions.Configuration.Json
Page 216 - Creating a class library that needs testing
Creating a class library project and adding it to the solution file:
dotnet new classlib -o CalculatorLib
dotnet sln add CalculatorLib
Creating an XUnit text project and adding it to the solution file:
dotnet new xunit -o CalculatorLibUnitTests
dotnet sln add CalculatorLibUnitTests
Running a unit test project:
dotnet test
Chapter 7 - Packaging and Distributing .NET Types
Page 369 - Checking your .NET SDKs for updates
Listing the installed .NET SDKs with a column to indicate if it has a newer version that can be upgraded to:
dotnet sdk check
Page 378 - Creating a .NET Standard 2.0 class library
Creating a new class library project that targets .NET Standard 2.0:
dotnet new classlib -f netstandard2.0
Page 379 - Controlling the .NET SDK
Listing the installed .NET SDKs:
dotnet --list-sdks
Creating a global.json file to control to default .NET SDK for projects created in the current folder and its descendents:
dotnet new globaljson --sdk-version 6.0.320
Page 384 - Understanding dotnet commands
Listing available project templates using .NET 7 or later:
dotnet new list
Listing available project templates using .NET 6 or earlier:
dotnet new --list
Listing available project templates using .NET 6 or earlier (short form):
dotnet new -l
Page 385 - Getting information about .NET and its environment
Getting detailed information about installed .NET runtimes, SDKs, and workloads:
dotnet --info
Page 386 - Publishing a self-contained app
Build and publish the release version for Windows:
dotnet publish -c Release -r win-x64 --self-contained
Build and publish the release version for macOS on Intel:
dotnet publish -c Release -r osx-x64 --self-contained
Build and publish the release version for macOS on Apple Silicon:
dotnet publish -c Release -r osx-arm64 --self-contained
Build and publish the release version for Linux on Intel:
dotnet publish -c Release -r linux-x64 --self-contained
Build and publish the release version for Linus on ARM64:
dotnet publish -c Release -r linux-arm64 --self-contained
Page 388 - Publishing a single-file app
dotnet publish -c Release -r win-x64 --no-self-contained /p:PublishSingleFile=true
dotnet publish -c Release -r win-x64 --self-contained /p:PublishSingleFile=true
Page 390 - Enabling assembly-level trimming
dotnet publish -c Release -r win-x64 --self-contained /p:PublishSingleFile=true -p:PublishTrimmed=True
Page 390 - Enabling type-level and member-level trimming
dotnet publish -c Release -r win-x64 --self-contained /p:PublishSingleFile=true -p:PublishTrimmed=True -p:TrimMode=Link
Page 395 - Publishing a native AOT project
dotnet publish
Chapter 9 - Working with Files, Streams, and Serialization
Page 508 - Expanding, setting, and getting an environment variables
To temporarily set an environment variable at the command prompt or terminal on macOS or Linux,
you can use the export command:
export MY_ENV_VAR=Delta
To set some environment variables at the user and machine scope levels on Windows:
setx MY_SECRET "Beta"
setx MY_SECRET "Gamma" /M
Chapter 10 - Working with Data Using Entity Framework Core
Page 518 - Setting up SQLite for macOS and Linux
On Linux, you can get set up with SQLite using the following command:
sudo apt-get install sqlite3
Page 519 - Creating the Northwind sample database for SQLite
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4SQLite.sql
Page 534 - Setting up the dotnet-ef tool
Listing installed dotnet global tools:
dotnet tool list --global
Updating an older dotnet-ef tool:
dotnet tool update --global dotnet-ef
Installing the latest dotnet-ef as a global tool:
dotnet tool install --global dotnet-ef
Uninstalling an older dotnet-ef tool:
dotnet tool uninstall --global dotnet-ef
Page 536 - Scaffolding models using an existing database
dotnet ef dbcontext scaffold "Data Source=Northwind.db" Microsoft.EntityFrameworkCore.Sqlite --table Categories --table Products --output-dir AutoGenModels --namespace WorkingWithEFCore.AutoGen --data-annotations --context NorthwindDb
Note the following:
- The command action:
dbcontext scaffold - The connection string:
"Data Source=Northwind.db" - The database provider:
Microsoft.EntityFrameworkCore.Sqlite - The tables to generate models for:
--table Categories --table Products - The output folder:
--output-dir AutoGenModels - The namespace:
--namespace WorkingWithEFCore.AutoGen - To use data annotations as well as the Fluent API:
--data-annotations - To rename the context from [database_name]Context:
--context NorthwindDb
Chapter 11 - Querying and Manipulating Data Using LINQ
Page 598 - Creating a console app for exploring LINQ to Entities
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4Sqlite.sql
Chapter 12 - Introducing Web Development Using ASP.NET Core
Page 635 - Creating a class library for entity models using SQLite
Creating the Northwind SQLite database:
sqlite3 Northwind.db -init Northwind4SQLite.sql
Creating the EF Core model for the Northwind database:
dotnet ef dbcontext scaffold "Data Source=../Northwind.db" Microsoft.EntityFrameworkCore.Sqlite --namespace Northwind.EntityModels --data-annotations
Chapter 13 - Building Websites Using ASP.NET Core Razor Pages
Page 660 - Testing and securing the website
Starting an ASP.NET Core project and specifying the https profile:
dotnet run --launch-profile https
Page 675 - Using code-behind files with Razor Pages
Creating a Razor Page named Suppliers.cshtml with a code-behind file:
dotnet new page -n Suppliers --namespace Northwind.Web.Page
Chapter 14 - Building and Consuming Web Services
Creating a Web API project using controllers:
dotnet new webapi --use-controllers -o Northwind.WebApi
Creating a Web API project using controllers (short form):
dotnet new webapi -controllers -o Northwind.WebApi
Creating a Web API project using Minimal APIs:
dotnet new webapi --use-minimal-api -o Northwind.WebApi
Creating a Web API project using Minimal APIs (short form):
dotnet new webapi -minimal -o Northwind.WebApi
Chapter 15 - Building User Interfaces Using Blazor
Page 749 - Creating a Blazor Web App project
Creating a new project using the Blazor Web App template with no server-side or client-side interactivity enabled by default:
dotnet new blazor --interactivity None -o Northwind.Blazor