زمان مطالعه: 3 دقیقه

.NET 6 API های جدید را برای توسعه ساده تر و سریعتر معرفی کرده که به توضیح آنها میپردازیم:

1- تا به حال دو کلاس DateTime , TimeSpan برای هندل کردن عملیات تاریخ وجود داشت اما د ر.NET 6 دو تابع DateOnly , TimeOnly برای کار راحت تر با تاریخ و زمان اضافه شده است

var dateOnly = new DateOnly(2021,7,7);
Assert.IsTrue(dateOnly.ToString() == "07-Jul-21");
Assert.IsTrue(dateOnly.AddMonths(1).ToString() == "07-Aug-21");
 
var timeOnly = new TimeOnly(11, 43, 57);
Assert.IsTrue(timeOnly.ToString() == "11:43 AM");
Assert.IsTrue(timeOnly.AddHours(1) > timeOnly);
Assert.IsTrue(timeOnly.AddHours(1) - timeOnly == new TimeSpan(1,0,0));
 
DateTime dateTime = dateOnly.ToDateTime(timeOnly);
Assert.IsTrue(dateTime.ToString() == "07-Jul-21 11:43:57 AM");
Assert.IsTrue(DateOnly.FromDateTime(dateTime) == dateOnly);
Assert.IsTrue(TimeOnly.FromDateTime(dateTime) == timeOnly);


2- برای پرفورمنس بالاتر صف اولویت دار PriorityQueue اضافه شده است که در آن اولویت آیتمها قبل تغییر نیست (سریعتر پیمایش می شود) و پیاده سازی آن هم الزاما پایدار نمی باشد. صف اولویت دار زمانی پایدار است که آیتمها به همان ترتیب برداشته شدن وارد صف شوند

var youngerFirstQueue = new PriorityQueue<string, int>();
youngerFirstQueue.Enqueue("Lena", 7);
youngerFirstQueue.Enqueue("Patrick", 46);
youngerFirstQueue.Enqueue("Paul", 7);
Assert.IsTrue(youngerFirstQueue.Dequeue() == "Lena");
Assert.IsTrue(youngerFirstQueue.Dequeue() == "Paul");
Assert.IsTrue(youngerFirstQueue.Dequeue() == "Patrick");


3- Index و Range در LINQ قابل استفاده شده اند.Index , Range از قابلیتهای C#8 هستند. توضیح

// 6 element indexed from 0 to 5
var arr = new [] {0, 1, 2, 3, 4, 5};
Assert.IsTrue(arr.ElementAt(^2) == 4); // Take the second element from the end
Assert.IsTrue(arr.ElementAtOrDefault(^10) == default); // No such index
Assert.IsTrue(arr.Take(2..4).SequenceEqual(new[] { 2, 3 }));
Assert.IsTrue(arr.Take(2..^2).SequenceEqual(new[] { 2, 3 }));
 
// New Index Range usage with their pre .NET 6 equivalent
Assert.IsTrue(arr.Take(..2).SequenceEqual(new[] { 0, 1 }));
Assert.IsTrue(arr.Take(2).SequenceEqual(new[] { 0, 1 }));
 
Assert.IsTrue(arr.Take(2..).SequenceEqual(new[] { 2, 3, 4, 5 }));
Assert.IsTrue(arr.Skip(2).SequenceEqual(new[] { 2, 3, 4, 5 }));
 
Assert.IsTrue(arr.Take(^2..).SequenceEqual(new[] { 4, 5 }));
Assert.IsTrue(arr.TakeLast(2).SequenceEqual(new[] { 4, 5 }));
 
Assert.IsTrue(arr.Take(..^2).SequenceEqual(new[] { 0, 1, 2, 3 }));
Assert.IsTrue(arr.SkipLast(2).SequenceEqual(new[] { 0, 1, 2, 3 }));

4- برای FirstOrDefault(), LastOrDefault() SingleOrDefault() میتوانید مقدار پیش فرض تعیین کنید


var arr = new [] {0, 1, 2, 3, 4, 5};
Assert.IsTrue(arr.FirstOrDefault(x => x > 6) == 0);
Assert.IsTrue(arr.FirstOrDefault(x => x > 6, -1) == -1);

5- قابلیتهای MaxBy(), MinBy(), DistinctBy(), UnionBy(), IntersectBy(), ExceptBy() اضافه شد اند

var buckets1 = new[] {
   (Color: "Red", Price: 7), 
   (Color: "Blue", Price: 10), 
   (Color: "Green", Price: 7),
};
var buckets2 = new[] {
   (Color: "White", Price: 7), 
   (Color: "Black", Price: 12),
};
Assert.IsTrue(buckets1.MaxBy(p => p.Price).Color == "Blue"); 
Assert.IsTrue(buckets1.MinBy(p => p.Price).Color == "Red");
 
// bucket from buckets1 distinct by price
Assert.IsTrue(buckets1.DistinctBy(p => p.Price)    
   .Select(p => p.Color).SequenceEqual(new [] {"Red", "Blue" }));
 
// Union from buckets1 and buckets2 distinct by price
Assert.IsTrue(buckets1.UnionBy(buckets2, p => p.Price)    
   .Select(p => p.Color).SequenceEqual(new[] { "Red", "Blue", "Black" }));
 
// Unique bucket from buckets1 with a price in buckets2
Assert.IsTrue(buckets1.IntersectBy(buckets2.Select(p => p.Price), p => p.Price)    
   .Select(p => p.Color).SequenceEqual(new[] { "Red" }));
 
// Unique bucket from buckets1 with a price Not in buckets2
Assert.IsTrue(buckets1.ExceptBy(buckets2.Select(p => p.Price), p => p.Price)    
   .Select(p => p.Color).SequenceEqual(new[] { "Blue"}));

6- چون ممکن است پیدا کردن تعداد پروفورمنس را تحت تاثیر بگذارد تابع TryGetNonEnumeratedCount برای تست کردن گرفتن تعداد طراحی شده که true, false می دهد

class MyCollection<T> : IEnumerable<T> {
   public IEnumerator<T> GetEnumerator() { throw new NotImplementedException(); }
   IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
 
...
 
IEnumerable<int> seq1 = new[] { 0, 1, 2, 3, 4, 5 };
Assert.IsTrue(seq1.TryGetNonEnumeratedCount(out int count1));
Assert.IsTrue(count1 == 6);
 
IEnumerable<int> seq2 = new MyCollection<int>();
Assert.IsFalse(seq2.TryGetNonEnumeratedCount(out int count2));

7- اضافه شدن IEnumerable Chunk و IQueryable Chunk و برای راحت تر کردن عملیات chunk کردن و خواندن توالی

var arr = new[] { 0, 1, 2, 3, 4, 5, 6 };
IEnumerable<int[]> chuncks = arr.Chunk(3);
Assert.IsTrue(chuncks.ElementAt(0).SequenceEqual(new[] { 0, 1, 2 }));
Assert.IsTrue(chuncks.ElementAt(1).SequenceEqual(new[] { 3, 4, 5 }));
Assert.IsTrue(chuncks.ElementAt(2).SequenceEqual(new[] { 6 }));

8- تا به حال می شد تا 2 توالی را Zip کنیم اما حالا امکان Zip کردن سه توالی اضافه شده است.

var integers = Enumerable.Range(0, 4);
var squares = integers.Select(i => i * i);
var cubes = integers.Select(i => i * i * i);
var zip = integers.Zip(squares, cubes).ToArray();
foreach ((int i, int square, int cube) in zip) {
   Assert.IsTrue($"{i} {square} {cube}" == $"{i} {i * i} {i * i * i}");
}

9- اضافه شدن EnsureCapacity() به List, Stack, Queue . باتوجه به اینکه همه این کالکشن ها یک ظرفیت داخلی دارند و با اضافه شدن به لیست آنها ظرفیت افزایش پیدا می کند و این کار می تواند در پرفورمنس موثر باشد، شما می توانید ظرفیت لازم را برای هر کدام تعیین کنید.

var list = new List<int> {1, 2};
Assert.IsTrue(list.Capacity < 100);
list.EnsureCapacity(100);
Assert.IsTrue(list.Count == 2);
Assert.IsTrue(list.Capacity == 100);
list.EnsureCapacity(50);
Assert.IsTrue(list.Capacity == 100);
for(int i = list.Count; i < 100; i++) {
   list.Add(i);
}
Assert.IsTrue(list.Count == 100);
Assert.IsTrue(list.Capacity == 100);

10- اضافه شدن متد جدید Task.WaitAsync() .اگر timeout رخ بدهد هم تسک و هم wait کنسل می شوند


public Task Task.WaitAsync(CancellationToken cancellationToken);
public Task Task.WaitAsync(TimeSpan timeout);
public Task Task.WaitAsync(TimeSpan timeout, CancellationToken cancellationToken)

منبع : https://blog.ndepend.com/top-10-new-net-6-0-api/

Visits: 1