interface IDog { void Bark(int seconds); }
扩展方法可添加到另一个类:
static class DogExtensions { // Barks for 2 seconds public static void Bark(this IDog dog) { dog.Bark(2); } }
接口实现器现在只需实现单一方法,但接口客户端却可以自由调用任一重载。
Close [x]
我们现在拥有了用于编写筛选子句的非常接近理想的语法,但“Orcas”版 C# 仅限于此吗?并不全然。让我们对示例稍作扩展,相对于整个客户对象,我们只投影出客户名称。如我前面所述,理想的语法应采用如下形式:
sequence locals =customers.where(ZipCode == 98112).select(Name);
IEnumerable locals = customers.Where(c => c.ZipCode == 91822).Select(c => c.Name);
locals = customers.where(ZipCode == 98112).select(Name, Address);
class CustomerTuple { public string Name; public string Address; public CustomerTuple(string name, string address) { this.Name = name; this.Address = address; } }
IEnumerable locals =customers.Where(c => c.ZipCode == 91822).Select(c => new CustomerTuple(c.Name, c.Address));拼吾爱