ویژگی جدید مورد بحث در این قسمت، «Improved Target Typing» نام دارد. اما «Target Typing» چیست؟ حدس زدن نوع یک شیء بر اساس زمینهای که توسط آن تعریف شدهاست، Target Typing نامیده میشود. نمونهای از آنرا سالهاست که با استفاده از واژهی کلیدی var در #C استفاده میکنید. اما قابلیتی که در C# 9.0 اضافه شدهاست، تقریبا معکوس آن است.
Target Typing در C# 9.0
مشکلی که بعضیها با واژهی کلیدی var دارند، این است که اندکی خوانایی کدها را کاهش میدهد و در این حالت بلافاصله مشخص نیست که نوع شیء در حال استفاده چیست. در C# 9.0 برای این دسته از برنامه نویسها راه حل دیگری را پیشنهاد دادهاند: نوع ابتدایی را مشخص کنید، اما نیازی به ذکر نوع پس از واژهی کلیدی new نیست و همانند var، خود کامپایلر آنرا حدس خواهد زد! برای توضیح آن دو کلاس سادهی زیر را درنظر بگیرید:
public class Person
{
public string FirstName { get; set; }
}
public class PersonWithCtor
{
public PersonWithCtor(string firstName)
{
this.FirstName = firstName;
}
public string FirstName { get; set; }
}
روش متداول استفادهی از کلاس Person ساده که بدون سازندهاست، از ابتداییترین نگارش #C به صورت زیر است:
Person person = new Person();
این روش در C# 3.0 به صورت زیر خلاصه شد:
var person = new Person();
که در این حالت کامپایلر در زمان کامپایل، واژهی کلیدی var را به صورت خودکار به نمونهی قبلی تبدیل کرده و عملیات کامپایل را تکمیل میکند. اگر با این روش تعریف متغیرها و اشیاء مشکل دارید و به نظرتان خوانایی آن کاهش یافته، میتوانید در C# 9.0 به صورت زیر عمل کنید:
در این حالت ابتدا نوع متغیر و یا شیء ذکر میشود. سپس در جائیکه قرار است new صورت گیرد، دیگر نیازی به تکرار آن نیست که به آن «Improved Target Typing» هم گفته میشود.
Target Typing و پارامترهای سازندهی کلاسها در C# 9.0
در مثال فوق، کلاس PersonWithCtor به همراه یک سازندهی پارامتردار تعریف شدهاست. در این حالت Target Typing آن به صورت زیر خواهد بود:
Person person = new("User 1");
و یا نمونهای از آن در حین تعریف مقادیر اولیهی Listها است:
var personList = new List<Person>
{
new ("User 1"),
new ("User 2"),
// ...
};
و یا حتی در حین تعریف پارامترهای یک متد نیز میتوان از target typing استفاده کرد و تنها به ذکر new بسنده نمود:
public void Adopt(Person p)
{
//...
}
public void CallerMethod()
{
this.Adopt(new Person("User 1"));
// C# 9.0
this.Adopt(new("User 1"));
}
نمونهی دیگری از این مثال را در حین مقدار دهی پارامتر دوم متد XmlReader.Create، در اینجا مشاهده میکنید:
XmlReader.Create(reader, new XmlReaderSettings() { IgnoreWhitespace = true });
// C# 9.0
XmlReader.Create(reader, new() { IgnoreWhitespace = true });
Target Typing و استفاده از خواص کلاسها در C# 9.0
در همان مثال اول، اگر بخواهیم خاصیت FirstName را مقدار دهی کنیم و همچنین از Target Typing نیز استفاده کنیم ... روش زیر کامپایل نخواهد شد:
Person person = new
{
FirstName = "User 2"
};
علت اینجا است که شیءای که پس از علامت انتساب قرارگرفتهاست، یک anonymous object است و قابلیت انتساب به نوع Person را ندارد. در این حالت تنها کافی است ذکر () را پس از new، فراموش نکرد؛ تا قطعه کد زیر بدون مشکل کامپایل شود:
Person person = new()
{
FirstName = "User 2"
};
امکان استفادهی از Target typing با فیلدها در C# 9.0
امکان تعریف var با فیلدهای یک کلاس در زبان #C وجود ندارد. به همین جهت مجبور هستیم یک چنین تعاریف طولانی را در سطح کلاسها داشته باشیم:
private ConcurrentDictionary<string, ObservableList<Cat>> _catsBefore = new ConcurrentDictionary<string, ObservableList<Cat>>();
اما با ارائهی C# 9.0، میتوان از target typing بر روی فیلدها نیز استفاده کرد و قطعه کد فوق را به صورت زیر خلاصه کرد:
private ConcurrentDictionary<string, ObservableList<Cat>> _cats = new(); // C# 9.0
این نکته در مورد مقدار دهی اولیهی خواص نیز صدق میکند:
public ObservableCollection<Friend> Friends { get; } = new();
امکان ترکیب null-coalescing operator با target typing در C# 9.0
null-coalescing operator یا همان ?? به این معنا است که اگر متغیر سمت چپ آن نال نبود، همان مقدار درنظر گرفته شود و اگر نال بود، متغیر سمت راست آن بازگشت داده شود. در این حالت مثال زیر را در نظر بگیرید که در آن سگ و گربه از نوع پایهی حیوان تعریف شدهاند:
public interface IAnimal
{
}
public class Dog : IAnimal
{
}
public class Cat : IAnimal
{
}
در اینجا میخواهیم اگر برای مثال cat نال بود، حاصل عملگر ?? به متغیری از نوع IAnimal قابل انتساب باشد:
Cat cat = null;
Dog dog = new();
IAnimal animal = cat ?? dog;
یک چنین کاری در نگارشهای پیشین #C مجاز نیست؛ اما در C# 9.0، چون target typeهای تعریف شده، قابل تبدیل به هم هستند، کامپایلر آنرا بدون مشکل کامپایل میکند (البته قرار است در نگارش نهایی آن این امر محقق شود؛ هنوز نه!).
دانستنیهایی در مورد Target Typing
- نوشتن ()throw new مجاز است و نوع پیشفرض آن، System.Exception در نظر گرفته میشود.
- در حالت کار با tuples، نوشتن new اضافی است:
(int a, int b) t = new(1, 2); // "new" is redundant
و همچنین اگر پارامترهای آن ذکر نشوند، با مقدار پیشفرض آن نوع جایگزین خواهند شد:
(int a, int b) t = new(); // OK; same as (0, 0)
محدودیتهای Target Typing در C# 9.0
- امکان نوشتن ()var dog = new وجود ندارد؛ چون نوع سمت راست این انتساب دیگر قابل حدس زدن نیست. نمونهی دیگر آن anonymous type properties است؛ مانند new { Prop = new() } که در آن برای مثال نوع خاصیت Prop قابل حدس زدن نیست.
- target typing با binary operators قابل استفاده نیست.
- به عنوان ref قابل استفاده نیست.