Microsoft.Extensions.Primitives 9.0.3

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Microsoft.Extensions.Primitives --version 9.0.3                
NuGet\Install-Package Microsoft.Extensions.Primitives -Version 9.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Extensions.Primitives" Version="9.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Extensions.Primitives --version 9.0.3                
#r "nuget: Microsoft.Extensions.Primitives, 9.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Extensions.Primitives as a Cake Addin
#addin nuget:?package=Microsoft.Extensions.Primitives&version=9.0.3

// Install Microsoft.Extensions.Primitives as a Cake Tool
#tool nuget:?package=Microsoft.Extensions.Primitives&version=9.0.3                

About

Microsoft.Extensions.Primitives contains isolated types that are used in many places within console or ASP.NET Core applications using framework extensions.

Key Features

  • IChangeToken: An interface that represents a token that can notify when a change occurs. This can be used to trigger actions or invalidate caches when something changes. For example, the configuration and file providers libraries use this interface to reload settings or files when they are modified.
  • StringValues: A struct that represents a single string or an array of strings. This can be used to efficiently store and manipulate multiple values that are logically a single value. For example, the HTTP headers and query strings libraries use this struct to handle multiple values for the same key.
  • StringSegment: A struct that represents a substring of another string. This can be used to avoid allocating new strings when performing operations on parts of a string. For example, the configuration and logging libraries use this struct to parse and format strings.

How to Use

IChangeToken with configuration example
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Create a configuration builder
        var configurationBuilder = new ConfigurationBuilder()
            .SetBasePath(Environment.CurrentDirectory)
            // appsettings.json expected to have the following contents:
            // {
            //   "SomeKey": "SomeValue"
            // }
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        // Build the configuration
        IConfiguration configuration = configurationBuilder.Build();

        // Create a change token for the configuration
        IChangeToken changeToken = configuration.GetReloadToken();

        // Attach a change callback
        IDisposable changeTokenRegistration = changeToken.RegisterChangeCallback(state =>
        {
            Console.WriteLine("Configuration changed!");
            IConfigurationRoot root = (IConfigurationRoot)state;
            var someValue = root["SomeKey"]; // Access the updated configuration value
            Console.WriteLine($"New value of SomeKey: {someValue}");
        }, configuration);

        // go and update the value of the key SomeKey in appsettings.json.
        // The change callback will be invoked when the file is saved.
        Console.WriteLine("Listening for configuration changes. Press any key to exit.");
        Console.ReadKey();

        // Clean up the change token registration when no longer needed
        changeTokenRegistration.Dispose();
    }
}
StringValues example
using System;
using Microsoft.Extensions.Primitives;

namespace StringValuesSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a StringValues object from a single string or an array of strings
            StringValues single = "Hello";
            StringValues multiple = new string[] { "Hello", "World" };

            // Use the implicit conversion to string or the ToString method to get the values
            Console.WriteLine($"Single: {single}"); // Single: Hello
            Console.WriteLine($"Multiple: {multiple}"); // Multiple: Hello,World

            // Use the indexer, the Count property, and the IsNullOrEmpty method to access the values
            Console.WriteLine($"Multiple[1]: {multiple[1]}"); // Multiple[1]: World
            Console.WriteLine($"Single.Count: {single.Count}"); // Single.Count: 1
            Console.WriteLine($"Multiple.IsNullOrEmpty: {StringValues.IsNullOrEmpty(multiple)}"); // Multiple.IsNullOrEmpty: False

            // Use the Equals method or the == operator to compare two StringValues objects
            Console.WriteLine($"single == \"Hello\": {single == "Hello"}"); // single == "Hello": True
            Console.WriteLine($"multiple == \"Hello\": {multiple == "Hello"}"); // multiple == "Hello": False
       }
    }
}

Main Types

The main types provided by this library are:

  • IChangeToken
  • StringValues
  • StringSegment

Additional Documentation

Feedback & Contributing

Microsoft.Extensions.Primitives is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (653)

Showing the top 5 NuGet packages that depend on Microsoft.Extensions.Primitives:

Package Downloads
Microsoft.Extensions.Options

Provides a strongly typed way of specifying and accessing settings using dependency injection.

Microsoft.Extensions.Configuration.Abstractions

Provides abstractions of key-value pair based configuration. Interfaces defined in this package are implemented by classes in Microsoft.Extensions.Configuration and other configuration packages.

Microsoft.Extensions.Configuration

Implementation of key-value pair based configuration for Microsoft.Extensions.Configuration. Includes the memory configuration provider.

Microsoft.Extensions.FileProviders.Abstractions

Abstractions of files and directories. Commonly Used Types: Microsoft.Extensions.FileProviders.IDirectoryContents Microsoft.Extensions.FileProviders.IFileInfo Microsoft.Extensions.FileProviders.IFileProvider

Microsoft.Extensions.FileProviders.Physical

File provider for physical files for Microsoft.Extensions.FileProviders.

GitHub repositories (179)

Showing the top 5 popular GitHub repositories that depend on Microsoft.Extensions.Primitives:

Repository Stars
dotnet/AspNetCore.Docs
Documentation for ASP.NET Core
chocolatey/choco
Chocolatey - the package manager for Windows
JeffreySu/WeiXinMPSDK
微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
microsoft/ailab
Experience, Learn and Code the latest breakthrough innovations with Microsoft AI
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
Version Downloads Last updated
10.0.0-preview.2.25163.2 4,701 3/18/2025
10.0.0-preview.1.25080.5 33,159 2/25/2025
9.0.3 1,452,549 3/11/2025
9.0.2 6,645,630 2/11/2025
9.0.1 9,944,906 1/14/2025
9.0.0 32,230,262 11/12/2024
9.0.0-rc.2.24473.5 839,114 10/8/2024
9.0.0-rc.1.24431.7 485,884 9/10/2024
9.0.0-preview.7.24405.7 470,409 8/13/2024
9.0.0-preview.6.24327.7 363,559 7/9/2024
9.0.0-preview.5.24306.7 253,051 6/11/2024
9.0.0-preview.4.24266.19 192,412 5/21/2024
9.0.0-preview.3.24172.9 384,458 4/11/2024
9.0.0-preview.2.24128.5 269,256 3/12/2024
9.0.0-preview.1.24080.9 271,365 2/13/2024
8.0.0 315,305,571 11/14/2023
8.0.0-rc.2.23479.6 2,759,139 10/10/2023
8.0.0-rc.1.23419.4 1,480,237 9/12/2023
8.0.0-preview.7.23375.6 851,911 8/8/2023
8.0.0-preview.6.23329.7 556,575 7/11/2023
8.0.0-preview.5.23280.8 421,912 6/13/2023
8.0.0-preview.4.23259.5 677,656 5/16/2023
8.0.0-preview.3.23174.8 662,143 4/11/2023
8.0.0-preview.2.23128.3 530,528 3/14/2023
8.0.0-preview.1.23110.8 434,962 2/21/2023
7.0.0 327,479,961 11/7/2022
7.0.0-rc.2.22472.3 2,195,776 10/11/2022
7.0.0-rc.1.22426.10 633,237 9/14/2022
7.0.0-preview.7.22375.6 482,828 8/9/2022
7.0.0-preview.6.22324.4 391,758 7/12/2022
7.0.0-preview.5.22301.12 256,273 6/14/2022
7.0.0-preview.4.22229.4 597,164 5/10/2022
7.0.0-preview.3.22175.4 243,208 4/13/2022
7.0.0-preview.2.22152.2 389,119 3/14/2022
7.0.0-preview.1.22076.8 264,476 2/17/2022
6.0.1 2,222,837 11/12/2024
6.0.0 727,756,339 11/8/2021
6.0.0-rc.2.21480.5 2,144,742 10/12/2021
6.0.0-rc.1.21451.13 825,627 9/14/2021
6.0.0-preview.7.21377.19 919,189 8/10/2021
6.0.0-preview.6.21352.12 460,102 7/14/2021
6.0.0-preview.5.21301.5 431,932 6/15/2021
6.0.0-preview.4.21253.7 2,053,131 5/24/2021
6.0.0-preview.3.21201.4 871,615 4/8/2021
6.0.0-preview.2.21154.6 308,930 3/11/2021 6.0.0-preview.2.21154.6 is deprecated because it is no longer maintained.
6.0.0-preview.1.21102.12 776,273 2/12/2021 6.0.0-preview.1.21102.12 is deprecated because it is no longer maintained.
5.0.1 58,162,512 4/6/2021 5.0.1 is deprecated because it is no longer maintained.
5.0.0 581,374,015 11/9/2020 5.0.0 is deprecated because it is no longer maintained.
5.0.0-rc.2.20475.5 1,204,802 10/13/2020 5.0.0-rc.2.20475.5 is deprecated because it is no longer maintained.
5.0.0-rc.1.20451.14 992,460 9/14/2020 5.0.0-rc.1.20451.14 is deprecated because it is no longer maintained.
5.0.0-preview.8.20407.11 743,713 8/25/2020 5.0.0-preview.8.20407.11 is deprecated because it is no longer maintained.
5.0.0-preview.7.20364.11 556,132 7/21/2020 5.0.0-preview.7.20364.11 is deprecated because it is no longer maintained.
5.0.0-preview.6.20305.6 263,145 6/25/2020 5.0.0-preview.6.20305.6 is deprecated because it is no longer maintained.
5.0.0-preview.5.20278.1 480,340 6/10/2020 5.0.0-preview.5.20278.1 is deprecated because it is no longer maintained.
5.0.0-preview.4.20251.6 452,446 5/18/2020 5.0.0-preview.4.20251.6 is deprecated because it is no longer maintained.
5.0.0-preview.3.20215.2 341,039 4/23/2020 5.0.0-preview.3.20215.2 is deprecated because it is no longer maintained.
5.0.0-preview.2.20160.3 457,306 4/2/2020 5.0.0-preview.2.20160.3 is deprecated because it is no longer maintained.
5.0.0-preview.1.20120.4 239,834 3/16/2020 5.0.0-preview.1.20120.4 is deprecated because it is no longer maintained.
3.1.32 29,777,054 12/13/2022
3.1.31 3,570,332 11/8/2022
3.1.30 4,008,261 10/11/2022
3.1.29 3,287,555 9/13/2022
3.1.28 7,061,926 8/9/2022
3.1.27 2,713,266 7/12/2022
3.1.26 2,317,915 6/14/2022
3.1.25 4,275,764 5/10/2022
3.1.24 15,099,888 4/11/2022
3.1.23 6,308,682 3/8/2022
3.1.22 26,835,273 12/14/2021
3.1.21 14,221,571 11/7/2021
3.1.20 7,769,724 10/11/2021
3.1.19 6,999,468 9/14/2021
3.1.18 70,713,330 8/10/2021
3.1.17 9,606,252 7/13/2021
3.1.16 17,542,059 6/8/2021
3.1.15 11,158,139 5/11/2021
3.1.14 23,201,401 4/6/2021
3.1.13 19,787,725 3/9/2021
3.1.12 15,463,564 2/9/2021
3.1.11 24,581,702 1/12/2021
3.1.10 37,907,550 11/9/2020
3.1.9 91,667,989 10/13/2020
3.1.8 268,420,892 9/8/2020
3.1.7 49,689,966 8/11/2020
3.1.6 55,533,367 7/14/2020
3.1.5 59,509,817 6/9/2020
3.1.4 74,310,595 5/12/2020
3.1.3 142,051,354 3/24/2020
3.1.2 93,583,017 2/18/2020
3.1.1 54,667,893 1/14/2020
3.1.0 337,102,000 12/3/2019
3.1.0-preview3.19553.2 2,146,359 11/13/2019 3.1.0-preview3.19553.2 is deprecated because it is no longer maintained.
3.1.0-preview2.19525.4 256,176 11/1/2019 3.1.0-preview2.19525.4 is deprecated because it is no longer maintained.
3.1.0-preview1.19506.1 1,364,440 10/15/2019 3.1.0-preview1.19506.1 is deprecated because it is no longer maintained.
3.0.3 79,324,248 2/18/2020 3.0.3 is deprecated because it is no longer maintained.
3.0.2 1,269,006 1/14/2020 3.0.2 is deprecated because it is no longer maintained.
3.0.1 8,914,583 11/18/2019 3.0.1 is deprecated because it is no longer maintained.
3.0.0 226,755,487 9/23/2019 3.0.0 is deprecated because it is no longer maintained.
3.0.0-rc1.19456.10 202,090 9/16/2019 3.0.0-rc1.19456.10 is deprecated because it is no longer maintained.
3.0.0-preview9.19423.4 1,819,140 9/4/2019 3.0.0-preview9.19423.4 is deprecated because it is no longer maintained.
3.0.0-preview8.19405.4 663,748 8/13/2019 3.0.0-preview8.19405.4 is deprecated because it is no longer maintained.
3.0.0-preview7.19362.4 381,022 7/23/2019 3.0.0-preview7.19362.4 is deprecated because it is no longer maintained.
3.0.0-preview6.19304.6 951,039 6/12/2019 3.0.0-preview6.19304.6 is deprecated because it is no longer maintained.
3.0.0-preview5.19227.9 900,742 5/6/2019 3.0.0-preview5.19227.9 is deprecated because it is no longer maintained.
3.0.0-preview4.19216.2 103,464 4/18/2019 3.0.0-preview4.19216.2 is deprecated because it is no longer maintained.
3.0.0-preview3.19153.1 462,903 3/6/2019 3.0.0-preview3.19153.1 is deprecated because it is no longer maintained.
3.0.0-preview.19074.2 266,108 1/29/2019 3.0.0-preview.19074.2 is deprecated because it is no longer maintained.
3.0.0-preview.18572.1 209,279 12/4/2018 3.0.0-preview.18572.1 is deprecated because it is no longer maintained.
2.2.0 819,016,510 12/3/2018 2.2.0 is deprecated because it is no longer maintained.
2.2.0-preview3-35497 824,297 10/17/2018 2.2.0-preview3-35497 is deprecated because it is no longer maintained.
2.2.0-preview2-35157 691,143 9/12/2018 2.2.0-preview2-35157 is deprecated because it is no longer maintained.
2.2.0-preview1-35029 377,622 8/22/2018 2.2.0-preview1-35029 is deprecated because it is no longer maintained.
2.1.6 8,221,394 11/13/2018
2.1.1 529,587,747 6/18/2018
2.1.0 599,690,297 5/29/2018
2.1.0-rc1-final 779,867 5/6/2018 2.1.0-rc1-final is deprecated because it is no longer maintained.
2.1.0-preview2-final 1,128,984 4/10/2018 2.1.0-preview2-final is deprecated because it is no longer maintained.
2.1.0-preview1-final 2,264,419 2/26/2018 2.1.0-preview1-final is deprecated because it is no longer maintained.
2.0.0 701,620,441 8/11/2017 2.0.0 is deprecated because it is no longer maintained.
2.0.0-preview2-final 863,202 6/28/2017 2.0.0-preview2-final is deprecated because it is no longer maintained.
2.0.0-preview1-final 839,186 5/10/2017 2.0.0-preview1-final is deprecated because it is no longer maintained.
1.1.1 86,026,048 5/9/2017 1.1.1 is deprecated because it is no longer maintained.
1.1.0 59,275,831 11/16/2016 1.1.0 is deprecated because it is no longer maintained.
1.1.0-preview1-final 290,003 10/24/2016 1.1.0-preview1-final is deprecated because it is no longer maintained.
1.0.1 148,419,876 12/12/2016 1.0.1 is deprecated because it is no longer maintained.
1.0.0 323,299,252 6/27/2016 1.0.0 is deprecated because it is no longer maintained.
1.0.0-rc2-final 2,380,603 5/16/2016 1.0.0-rc2-final is deprecated because it is no longer maintained.
1.0.0-rc1-final 1,067,658 11/18/2015 1.0.0-rc1-final is deprecated because it is no longer maintained.