Updated: 28 January 2023
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.
var v = new { Amount = 108, Message = "Hello" };
Freelance software engineer United Kingdom
Updated: 28 January 2023
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.
var v = new { Amount = 108, Message = "Hello" };
Updated: 28 January 2023
A lambda expression is used to create an anonymous function. The lambda declaration operator =>
separates the lambda’s parameter list from its body.
Expression lambda. Returns the result of the expression
// (input-parameters) => expression
Statement lambda
// (input-parameters) => { <statement block> }
Action<string> greet = name =>
{
string greeting = $"Hello {name}";
Console.WriteLine(greeting);
};
// greet("World");
Input parameters
// none
Action line = () => Console.WriteLine();
// one
Func cube = x => x * x * x;
Func cube = (x) => x * x * x;
// two or more
Func testForEquality = (x, y) => x == y;
// with types
Func isTooLong = (int x, string s) => s.Length > x;
Discards specify input parameters which are not used in the expression
Func<int, int, int> constant = (_, _) => 42;
Updated: 06 March 2025
Establish a development environment inside a running container
# Try this
docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:6.0
# or try this
docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:7.0
# or this
docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:8.0
# or this
docker run -it \
--volume $(pwd):/source \
--workdir /source \
mcr.microsoft.com/dotnet/sdk:9.0
# Jan 2025, image sdk:10.0 does not exist.
Create a hello world project
# from inside the container
dotnet new console --framework net7.0 --use-program-main --name myapp
Run the app
cd myapp
dotnet run
https://github.com/dotnet/dotnet-docker/blob/main/samples/dotnetapp/Dockerfile
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# copy and publish app and libraries
COPY . .
RUN dotnet publish -c Release -o /app --self-contained false --no-restore
# final stage/image
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "source.dll"]
Build image based on Dockerfile above, in the same directory
docker build -t dndev .
Run a container
docker run dndev
Updated: 30 June 2023
Add a package
dotnet add package System.Data.OleDb --version 8.0.0-preview.5.23280.8
Remove package reference from a project file.
dotnet remove package System.Data.OleDb
Lists the package references for a project or solution.
dotnet list package
Updated: 24 January 2023
Consider
Foo[] array; List<Foo> list;
Updated: 24 January 2023
Try .NET In-Browser and enter focus mode.
Updated: 16 January 2025
.NET is an open source developer platform (i.e. the Languages & Libraries) for building different types of apps. The supported languages are C#, Visual Basic and F#.
Microsoft .NET and .NET Core Release History
Target platforms are:
https://docs.microsoft.com/dotnet
https://aka.ms/dotnethelloworld
A container which offers built-in services:
.NET 6 offers Generic DefaultHost
which can be configured for the application use cases.
A service which performs work in the background. Any reference type object which implements IHostedService
is a background / hosted / worker service.
Class dependencies can be registered in a service container.
.NET provides a built-in service container, IServiceProvider
. Services are typically registered at the app’s start-up and appended to an IServiceCollection
. Once all services are added BuildServiceProvider
is used to create the service container.
Dependency Injection injects the service into the constructor of the class where it’s needed. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it’s no longer needed.
Updated: 09 May 2025
Add the PrimeService.Tests
project to the current solution file
dotnet sln add ./PrimeService.Tests/PrimeService.Tests.csproj
Add a reference to lib.csproj
inside app.csproj
dotnet add app/app.csproj reference lib/lib.csproj
Add or updates a package reference in a project file
dotnet add package docopt.net
Creates a new project, configuration file, or solution, based on the specified template. Templates which come pre-installed with the .NET SDK
Initiate a project for creating a command-line application
dotnet new console --name our_project
Initiate a project for creating a command-line application and generate an explicit Program class and Main method instead of top-level statements.
dotnet new console --name file-encrypt --use-program-main
Display a list of all installed templates
dotnet new list
Create a Class Library project
dotnet new classlib --name MyClassLib
Build and test the Release version of a console application
dotnet run --configuration Release
Run the tests in the project in the current directory
dotnet test
Runs tests whose fully qualified name contains theMethod
dotnet test --filter theMethod
Clean up the output of the previous build. Both intermediate (obj) and final output (bin) folders are cleaned.
dotnet clean
Restores the dependencies and tools for a project i.e. external libraries in NuGet packages. All dependencies become available in a local cache and can be used by the .NET CLI to build and run the application.
dotnet restore
List installed Software Developer Kits
dotnet --list-sdks
List installed runtimes
dotnet --list-runtimes
Build a project and its dependencies using Release configuration
dotnet build --configuration Release
Build the project and its dependencies into a set of binaries.
The binaries include the project’s code in Intermediate Language (IL) files with a .dll extension.
dotnet build
Publishes the application and its dependencies to a folder for deployment to a hosting system.
dotnet publish
Publish the app in Release configuration
dotnet publish --configuration Release
Attempt to target Linux
dotnet publish --runtime linux-x64 -p:PublishSingleFile=true --self-contained false
Formats code to match .editorconfig
settings.
Verifies that no formatting changes would be performed. Terminates with a non zero exit code if any files would have been formatted
dotnet format --verify-no-changes
Only check the test
directory
dotnet format --include=test/
One or more optional workloads can be installed on top of the .NET SDK, to provide support for various application types, such as .NET MAUI and Blazor
dotnet workload install maui