You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To add content, fill out the following template as a new comment on this issue. Last day to submit content is the Friday before release.
## Contribution Template - Use to your discretion
Feature Name: [Short title of the feature]
What It Does: [Brief description of the feature]
How It Helps: [Explain the benefit or problem it solves]
How to Use: [Simple instructions on using the feature]
Future Plans: [Any upcoming updates for this feature, if any]
Documentation: [A link for readers to learn more]
System.Linq: CountBy, AggregateBy and Index methods
We've added new Linq methods that make it possible to aggregate state by key, without needing to allocate intermediate groupings via GroupBy. You can use CountBy to quickly calculate the frequency for each key:
stringsourceText=""" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices amet diam. """;// Find the most frequent word in a piece of textKeyValuePair<string,int>mostFrequentWord=sourceText.Split(newchar[]{' ','.',','},StringSplitOptions.RemoveEmptyEntries).Select(word =>word.ToLowerInvariant()).CountBy(word =>word).MaxBy(pair =>pair.Value);Console.WriteLine(mostFrequentWord.Key);// amet
More general-purpose workflows can be implemented using the new AggregateBy method, here's how we can calculate scores by a given key:
While it wouldn't be possible to implement efficient $\mathcal O(\log n)$ priority updates in the existing collection, the new Remove method makes it possible to emulate priority updates (albeit at $\mathcal O(n)$ time):
publicstaticvoidUpdatePriority<TElement,TPriority>(thisPriorityQueue<TElement,TPriority>queue,TElementelement,TPrioritypriority){queue.Remove(element,out_);// Scan the heap for entries matching the current elementqueue.Enqueue(element,priority);// Now re-insert it with the new priority.}
This unblocks users looking to implement graph algorithms in contexts where asymptotic performance isn't a blocker (e.g. education or prototyping). For example, here's a toy implementation of Dijkstra's algorithm using the new API.
.NET 9 now provides the KMAC algorithm, as specified by NIST SP-800-185. KMAC is a pseudo random function and keyed hash function based on Keccak.
The KMAC algorithm can be used with the Kmac128, Kmac256, KmacXof128, and KmacXof256 classes all in the System.Security.Cryptography namespace. Instances can be created to accumulate data to produce a MAC, or use the static method HashData for a one-shot over a single input.
KMAC is available on Linux with OpenSSL 3.0 or newer, and Windows 11 Build 26016 or newer. The static IsSupported property can be used to determine if the platform supports the desired algorithm.
usingSystem.Security.Cryptography;if(Kmac128.IsSupported){byte[]key=GetKmacKey();byte[]input=GetInputToMac();byte[]mac=Kmac128.HashData(key,input,outputLength:32);}else{// Handle scenario where KMAC is not available.}
CryptographicOperations.HashData
Over the past few releases of .NET, static one-shot implementations of hash functions, and related functions, has been implemented such as SHA256.HashData or HMACSHA256.HashData. One-shots are preferable to use because they can provide the best possible performance and reduce or eliminate allocations.
If a developer wishes to provide an API which supports hashing where the caller defines which hash algorithm to use, this is typically done by accepting a HashAlgorithmName as a parameter to the API. However, using this with one-shots would require switching over every possible HashAlgorithmName and using the appropriate method.
Starting in .NET 9, the CryptographicOperations.HashData API can be used to produce a hash or HMAC over an input as a one-shot where the algorithm used is determined by a HashAlgorithmName.
Reflection Emit and AssemblyBuilder have been in .NET for many years, but with the introduction of .NET Core and .NET 5+ support was limited to a runnable AssemblyBuilder. Adding support for saving an assembly has been asked since the first release of .NET Core and it's been the most upvoted issue in the Reflection area. Many customers report it as a blocker for porting their project form .NET Framework to .NET 6+.
Prototyping was done in .NET 7.0 by @MosheWolberg to investigate the feasibility of adding support for Save() via an adapter from AssemblyBuilder using the MetadataBuilder APIs that supports writing IL to a Stream\file.
The new persisted AssemblyBuilder implementation is runtime/platform independent. For creating new persisted AssemblyBuilder instance you need to use a new API AssemblyBuilder.DefinePersistedAssembly. As the existing AssemblyBuilder.DefineDynamicAssembly API accepts AssemblyName instance as fist argument, and optional custom attributes as last argument. The difference is you need to pass the core assembly, System.Private.CoreLib, that would be used for referencing base runtime types, no option for AssemblyBuilderAccess, for now the new persisted AssemblyBuilder implementation only supports saving, not running. After creating an instance of the persisted AssemblyBuilder further steps for defining a module/type/method/enum etc., writing IL, and all other usages are kept the same as the existing runtime Reflection Emit. That means you should be able to use existing Emit code as is for saving the assembly. Here is a simple example:
publicvoidCreateAndSaveAssembly(stringassemblyPath){AssemblyBuilderab=AssemblyBuilder.DefinePersistedAssembly(newAssemblyName("MyAssembly"),typeof(object).Assembly);TypeBuildertb=ab.DefineDynamicModule("MyModule").DefineType("MyType",TypeAttributes.Public|TypeAttributes.Class);MethodBuildermb=tb.DefineMethod("SumMethod",MethodAttributes.Public|MethodAttributes.Static,typeof(int),[typeof(int),typeof(int)]);ILGeneratoril=mb.GetILGenerator();il.Emit(OpCodes.Ldarg_0);il.Emit(OpCodes.Ldarg_1);il.Emit(OpCodes.Add);il.Emit(OpCodes.Ret);tb.CreateType();ab.Save(assemblyPath);// or could save to a Stream}publicvoidUseAssembly(stringassemblyPath){Assemblyassembly=Assembly.LoadFrom(assemblyPath);Typetype=assembly.GetType("MyType");MethodInfomethod=type.GetMethod("SumMethod");Console.WriteLine(method.Invoke(null,[5,10]));}
Future Plans: A few missing API implementations and bunch of bug fixes will added in preview 2. Further Entry point support will be added soon, probably within preview 2.
Activity
eiriktsarpalis commentedon Jan 26, 2024
System.Text.Json improvements
Customizing indent character and indent size
It is now possible to customize the indentation character and size of written JSON:
JsonSerializerOptions.WebWe now include a
JsonSerializerOptionssingleton configured to useJsonSerializerDefaults.Web:eiriktsarpalis commentedon Jan 26, 2024
System.Linq:
CountBy,AggregateByandIndexmethodsWe've added new Linq methods that make it possible to aggregate state by key, without needing to allocate intermediate groupings via
GroupBy. You can useCountByto quickly calculate the frequency for each key:More general-purpose workflows can be implemented using the new
AggregateBymethod, here's how we can calculate scores by a given key:The new
Indexmethod makes it possible to quickly extract the implicit index of an enumerable. The code:Now becomes
eiriktsarpalis commentedon Jan 26, 2024
PriorityQueue.RemoveBack in .NET 6 we shipped a
PriorityQueuecollection that provides a simple and fast array heap implementation. One issue however with array heaps in general is that they don't support priority updates, making them prohibitive for use in algorithms such as variations of Dijkstra's algorithm.While it wouldn't be possible to implement efficient$\mathcal O(\log n)$ priority updates in the existing collection, the new $\mathcal O(n)$ time):
Removemethod makes it possible to emulate priority updates (albeit atThis unblocks users looking to implement graph algorithms in contexts where asymptotic performance isn't a blocker (e.g. education or prototyping). For example, here's a toy implementation of Dijkstra's algorithm using the new API.
vcsjones commentedon Jan 30, 2024
System.Security.Cryptography
KMAC.NET 9 now provides the KMAC algorithm, as specified by NIST SP-800-185. KMAC is a pseudo random function and keyed hash function based on Keccak.
The KMAC algorithm can be used with the
Kmac128,Kmac256,KmacXof128, andKmacXof256classes all in theSystem.Security.Cryptographynamespace. Instances can be created to accumulate data to produce a MAC, or use the static methodHashDatafor a one-shot over a single input.KMAC is available on Linux with OpenSSL 3.0 or newer, and Windows 11 Build 26016 or newer. The static
IsSupportedproperty can be used to determine if the platform supports the desired algorithm.CryptographicOperations.HashDataOver the past few releases of .NET, static one-shot implementations of hash functions, and related functions, has been implemented such as
SHA256.HashDataorHMACSHA256.HashData. One-shots are preferable to use because they can provide the best possible performance and reduce or eliminate allocations.If a developer wishes to provide an API which supports hashing where the caller defines which hash algorithm to use, this is typically done by accepting a
HashAlgorithmNameas a parameter to the API. However, using this with one-shots would require switching over every possibleHashAlgorithmNameand using the appropriate method.Starting in .NET 9, the
CryptographicOperations.HashDataAPI can be used to produce a hash or HMAC over an input as a one-shot where the algorithm used is determined by aHashAlgorithmName.buyaa-n commentedon Feb 6, 2024
System.Reflection.Emit
Feature Name: Support equivalent of AssemblyBuilder.Save
What It Does and How It Helps:
Reflection Emit and AssemblyBuilder have been in .NET for many years, but with the introduction of .NET Core and .NET 5+ support was limited to a runnable AssemblyBuilder. Adding support for saving an assembly has been asked since the first release of .NET Core and it's been the most upvoted issue in the Reflection area. Many customers report it as a blocker for porting their project form .NET Framework to .NET 6+.
Prototyping was done in .NET 7.0 by @MosheWolberg to investigate the feasibility of adding support for
Save()via an adapter fromAssemblyBuilderusing theMetadataBuilderAPIs that supports writing IL to a Stream\file.In .NET 8 we added around half of the implementation. Now in .NET 9 preview 1 we are finishing the implementation of persisted
AssemblyBuilderand adding public APIs for saving an assembly.How to Use:
The new persisted
AssemblyBuilderimplementation is runtime/platform independent. For creating new persistedAssemblyBuilderinstance you need to use a new APIAssemblyBuilder.DefinePersistedAssembly. As the existingAssemblyBuilder.DefineDynamicAssemblyAPI acceptsAssemblyNameinstance as fist argument, and optional custom attributes as last argument. The difference is you need to pass the core assembly,System.Private.CoreLib, that would be used for referencing base runtime types, no option forAssemblyBuilderAccess, for now the new persistedAssemblyBuilderimplementation only supports saving, not running. After creating an instance of the persistedAssemblyBuilderfurther steps for defining a module/type/method/enum etc., writing IL, and all other usages are kept the same as the existing runtime Reflection Emit. That means you should be able to use existing Emit code as is for saving the assembly. Here is a simple example:Future Plans: A few missing API implementations and bunch of bug fixes will added in preview 2. Further Entry point support will be added soon, probably within preview 2.
CC @jkotas @AaronRobinsonMSFT @steveharter @ericstj @jeffhandley in case you want to add more
gewarren commentedon Feb 8, 2024
@eiriktsarpalis Do the new indentation options apply to Utf8JsonWriter as well?
eiriktsarpalis commentedon Feb 8, 2024
@gewarren it does, equivalent options are exposed in the
JsonWriterOptionsstruct. I've omitted use of those here.co-nl-on commentedon Feb 13, 2024
I believe the second argument should be
(totalScore, curr) => totalScore + curr.scorerichlander commentedon Feb 13, 2024
Thanks @co-nl-on. Got that fixed here: https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview1/libraries.md
Coding-Lambda commentedon Feb 14, 2024
Shouldn't
string lineandint indexbe swapped, according to this issue comment and the merged PR changes of that issue?eiriktsarpalis commentedon Feb 14, 2024
@Coding-Lambda Thanks, the sample has been updated #9186