Member-only story
10 powerfull hidden features of C#
ValueTask
A novel addition to the .NET Core framework is the ValueTask, which can optimize the execution of asynchronous code, elevating its performance. ValueTask bears resemblance to a Task, however, it is a value type, contrary to Task which is a reference type. This unique attribute of ValueTask eradicates the associated expenses linked with memory allocation and deallocation of the Task object.
Example:
public static async ValueTask<int> GetAsync()
{
await Task.Delay(100);
return 42;
}
public static async Task Main()
{
int result = await GetAsync();
Console.WriteLine(result);
}
SIMD
SIMD or Single Instruction Multiple Data is a method utilized to facilitate concurrent processing of data. The .NET Core framework extends support for SIMD instructions, allowing for heightened performance in certain types of computation, including image processing and scientific computing.
Example:
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
public static float Sum(float[] values)
{
Vector<float> sum = Vector<float>.Zero;
int i = 0;
for (; i <= values.Length - Vector<float>.Count; i += Vector<float>.Count)
{
sum += new Vector<float>(values, i);
}
float total =…