دات نت 6 یک API سطح ذیل نو برای تلاوت و تایپ کردن پوشه ها
طراحی سایت در مشهد سوای FileStream معرفی مینماید. یک نوع نو، RandomAccess، یک API ای برای قرائت و تایپ کردن فولدرها با یک طرز thread-safe ارائه میدهد.
using SafeFileHandle handle = File.OpenHandle("file.txt", access: FileAccess.ReadWrite);
// Write to file
byte[] strBytes = Encoding.UTF8.GetBytes("Hello world");
ReadOnlyMemory buffer1 = new(strBytes);
await RandomAccess.WriteAsync(handle, buffer1, 0);
// Get file length
long length = RandomAccess.GetLength(handle);
// Read from file
Memory buffer2 = new(new byte[length]);
await RandomAccess.ReadAsync(handle, buffer2, 0);
string content = Encoding.UTF8.GetString(buffer2.ToArray());
Console.WriteLine(content); // Hello world
PeriodicTimer تازه
با یک PeriodicTimer به طور کامل ناهمزمان (async) آشنا گردید.
طراحی سایت به شما این قابلیت و امکان را می دهد تا به صورت ناهمزمان چشم به راه تیک های زمانسنج بمانید. PeriodicTimer یک روال دارااست، WaitForNextTickAsync، که درانتظار
رایاپارس تیک آتی زمانسنج یا این که توقف زمانسنج میماند.
// One constructor: public PeriodicTimer(TimeSpan period)
using PeriodicTimer timer = new(TimeSpan.FromSeconds(1));
while (await timer.WaitForNextTickAsync())
{
Console.WriteLine(DateTime.UtcNow);
}
// Output:
// 13 - Oct - 21 19:58:05 PM
// 13 - Oct - 21 19:58:06 PM
// 13 - Oct - 21 19:58:07 PM
// 13 - Oct - 21 19:58:08 PM
// 13 - Oct - 21 19:58:09 PM
// 13 - Oct - 21 19:58:10 PM
// 13 - Oct - 21 19:58:11 PM
// 13 - Oct - 21 19:58:12 PM
// ...
Configuration Helper تازه
یک helper نو کانفیگ، GetRequiredSection، به دات نت 6 اضافه شدهاست. چنانچه که قسمت کانفیگ مایحتاج وجود نداشته باشد، یک اکسپشن ارسال می شود.
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
MySettings mySettings = new();
// Throws InvalidOperationException if a required section of configuration is missing
app.Configuration.GetRequiredSection("MySettings").Bind(mySettings);
app.Run();
class MySettings
{
public string? SettingValue { get; set; }
}
Native Memory API
دات نت 6 یک API نو اختصاص خاطر ساکن (Native Memory) معرفی مینماید.
ahrefs نوع تازه NativeMemory متدهایی برای اختصاص و آزادسازی خاطر داراست.
unsafe
{
byte* buffer = (byte*)NativeMemory.Alloc(100);
NativeMemory.Free(buffer);
/* This class contains methods that are mainly used to manage native memory.
public static class NativeMemory
{
public unsafe static void* AlignedAlloc(nuint byteCount, nuint alignment);
public unsafe static void AlignedFree(void* ptr);
public unsafe static void* AlignedRealloc(void* ptr, nuint byteCount, nuint alignment);
public unsafe static void* Alloc(nuint byteCount);
public unsafe static void* Alloc(nuint elementCount, nuint elementSize);
public unsafe static void* AllocZeroed(nuint byteCount);
public unsafe static void* AllocZeroed(nuint elementCount, nuint elementSize);
public unsafe static void Free(void* ptr);
public unsafe static void* Realloc(void* ptr, nuint byteCount);
}*/
}