In this programming tutorial, we take a look at the .NET Community Toolkit version 8, its new features, and updates to the developer tool.
What is the .NET Community Toolkit?
The .NET Community Toolkit (which was originally part of the Windows Community Toolkit) is simply a collection of APIs and other helpers. The Toolkit forms part of the .NET foundation and is system and platform agnostic. These libraries multi-target from .NET Standard 2.0 to .NET 6.
The .NET Community Toolkit is available on GitHub and includes every library in the Toolkit. These libraries include:
CommunityToolkit.HighPerformance contains helpers that allow developers to work more efficiently with high-performance code. It also includes the following APIs:
- Pooled buffer helpers: These represent a pool of memory blocks.
- String pool type: The StringPool type implements a configurable pool for string instances to minimize memory allocation when multiple char or byte string instances are created.
- 2D variant of Memory<T> – Memory2D<T>: Memory2D<T> contains the same functionality as the Memory type, and can be used to represent 2D memory locations.
- 2D variant of <Span<T> – Span2D<T>: Span2D<T> contains the same functionality as the <Span<T> type, but it supports 2D memory regions.
CommunityToolkit.Common contains helper APIs that are shared with other libraries in the Community Toolkit. These include the following:
- CommunityToolkit.Common.Parsers Namespace
- CommunityToolkit.Common.Deferred Namespace
- CommunityToolkit.Common.Collections Namespace
- CommunityToolkit.Common.Parsers.Core Namespace
- CommunityToolkit.Common.Parsers.Markdown Namespace
- CommunityToolkit.Common.Parsers.Markdown.Blocks Namespace
- CommunityToolkit.Common.Parsers.Markdown.Render Namespace
- CommunityToolkit.Common.Parsers.Markdown.Inlines Namespace
- CommunityToolkit.Common.Converters Class
- CommunityToolkit.Common.TaskExtensions Class
- CommunityToolkit.Common.ArrayExtensions Class
- CommunityToolkit.Common.StringExtensions Class
- CommunityToolkit.Common.Parsers.IParser Interface
- CommunityToolkit.Common.Parsers.SchemaBase Class
- CommunityToolkit.Common.Deferred.EventDeferral Class
- CommunityToolkit.Common.Parsers.Core.ParseHelpers Class
- CommunityToolkit.Common.Deferred.DeferredEventArgs Class
- CommunityToolkit.Common.Parsers.Markdown.ListStyle Enum
- CommunityToolkit.Common.Parsers.Markdown.HyperlinkType Enum
- CommunityToolkit.Common.Deferred.EventHandlerExtensions Class
- CommunityToolkit.Common.Deferred.DeferredCancelEventArgs Class
- CommunityToolkit.Common.Parsers.Markdown.ColumnAlignment Enum
- CommunityToolkit.Common.Parsers.Markdown.MarkdownElement Class
- CommunityToolkit.Common.Parsers.Core.StringValueAttribute Class
CommunityToolkit.Diagnostics contains helper APIs that deal with more efficient argument validation and error checking. Some APIs include:
Guard validates method arguments smoothly, thus, making it faster, less verbose, less error-prone, and more expressive, saving developers from writing checks and throwing exceptions manually.
The Guard APIs are based on the following three principles:
- Speed: The methods in Guard APIs are generated by making use of T4 templates. As per my book on Visual Studio 2019: “A T4 text template or pre-processed template is a combination of text blocks and control logic that can generate a text file. The logic is written as fragments of program code.”
There are two types of pre-processed text templates: Design Time (Design-time T4 text templates generate program code in a Visual Studio project) and Runtime (we can generate text strings in our applications during runtime using runtime T4 text templates.)
- Being intuitive: All Guard APIs have simple self-explanatory names that make them easy to identify and deduce their purposes.
- Helpfulness: Guard APIs supply detailed exception messages. For more information on Guard APIs have a look at their official documentation.
- ThrowHelper: As its name implies, the ThrowHelper class helps to efficiently throw exceptions. This class is best used in conjunction with the Guard APIs, especially if programmers want detailed control of the exceptions being thrown.
The MVVM is built around the following principles, according to Microsoft:
- Platform and Runtime Independent: .NET Standard 2.0 and .NET 5 (UI Framework Agnostic)
- Simple to pick up and use: No strict requirements on Application structure or coding-paradigms (outside of ‘MVVM’ness); i.e., flexible usage.
- À la carte: Freedom to choose which components to use.
- Reference Implementation: Lean and performant, providing implementations for interfaces that are included in the Base Class Library, but lack concrete types to use them directly.
For more information on Microsoft.Toolkit.Mvvm, read its official documentation.