扩展方法让您能够向现有类型添加方法(包括您不拥有的类型,如 string 或 int),而无需修改它们或进行继承。它们被定义为静态方法,其第一个参数上带有 this 修饰符,并像实例方法一样被调用。LINQ 就是构建在它们之上的。
定义扩展方法
{
{
str.Contains() && str.Contains();
}
{
str.Length <= max ? str : str[..max] + ;
}
}
.IsValidEmail();
.Truncate();
