在C#13中,对params的定义类型进行了扩充,不仅仅是数组了,开始支持集合了。IEnumerable<T>,IList<T>,List<T>,Span<T>,ReadOnlySpan<T>都得到了支持。这亲就不用每次进行一次转换工作了。实例如下:
F0(1, 2, 3, 4);
F1(1, 2, 3, 4);
F2(1, 2, 3, 4);
F3(1, 2, 3, 4);
F4(1, 2, 3, 4);
F5(1, 2, 3, 4);
static void F0(params int[] arr)
{
foreach (var item in arr)
{
Console.WriteLine(item);
}
}
static void F1(params List<int> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
static void F2(params IList<int> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
static void F3(params IEnumerable<int> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
static void F4(params Span<int> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
static void F5(params ReadOnlySpan<int> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
另一个是对正则的源生成器进行了扩展,不仅仅是方法,这次加上了属性,我个人感觉属性是更优雅的做法,毕竟正则一般是约束数据格式的,与属性的匹配度要高一些。
var email = new EmailDemo();
email.SendEmail("abc@aaa");
email.EMail = "abc@gmail.com";
public partial class EmailDemo
{
[ ]
private static partial Regex ValidEMail();
public void SendEmail(string email)
{
if (!ValidEMail().IsMatch(email))
{
Console.WriteLine("emal不正确");
return;
}
Console.WriteLine("发送EMail");
}
[ ]
private static partial Regex ValidateEMail { get; }
private string _email;
public string EMail
{
get
{
return _email;
}
set
{
if (ValidateEMail.IsMatch(value))
{
_email = value;
}
else
{
Console.WriteLine("emal不正确");
}
}
}
}