软件频道>程序开发>JavaVBVCDelphiC/C++Web开发微软专栏移动数据库程序人生软件工程|开发客
您现在的位置: 天极网 > 开发频道 > C#4.0的4个新特性
全文

C#4.0的4个新特性

2008-07-26 07:00作者:Gray Zhang 出处:天极网责任编辑:nancy

  1. 通过委托成员来实现接口

  我很想看到在C# 4.0中可以通过委托某个成员的实现来实现一个接口,例如下面的代码:

 //faux code
  public class Foo : IList
  {
  private List _Collection implements IList;
  public Foo()
  {
  _Collection = new List();
  }
  }

  被封闭的成员可以用以委托实现一个或多个接口,多个接口用逗号分隔。这么做可以少掉很多冗余的代码,就像上面的示例,不再需要在封闭类(Foo类)上写一大堆方法来将接口实现交给成员变量,接口的实现会直接映射到受委托的接口实现(_Collection成员变量)。这个功能同样增强了对minxins的支持。

  这个就是“委托模式”了,wikipedia上对此模式的解释如下:

  委托模式是指一个对象对外表现某种行为,但事实上只是将实现此行为的任务将会给一个相关的成员的技术,这种技术反转了责任。委托模式是加强组合 (聚合)、minxins及aspects的一种基本模式。

  再进一步,在委托实现接口之余,我们也应当可以自由地重写某些方法如下:

public class Foo : IList
  {
  private List _Collection { get; set; } implements IList;
  public Foo()
  {
  _Collection = new List();
  }
  //This would override the delegated implementation
  // for nice mixin functionality and easy decorator pattern implementation
  public int IList.Add(string value)
  {
  if (!_Collection.Contains(value))
  _Collection.Add(value);
  }
  }

  2. 匿名返回类型

  我希望见到在C#中匿名类型可以拥有像普通的类声明一样的地位。(当前)匿名类型只能用于局部变量,不能作为方法的返回值。但是如果一个强类型的LINQ查询的返回类型可以作为方法的返回类型一定很好,比如下面的代码:

//faux code
  public var GetProductInfos()
  {
  var productInfos =
  from p in products
  select new { p.ProductName, p.Category, Price = p.UnitPrice };
  return productInfos;
  }

  3. Some Duck-typing or Structural Subtyping support

  如果一个类中的某一个方法/属性的签名和某个接口一样,并且这个类没有实现此接口,那么这个类就将隐式地实现这个接口。只有这个类实现了接口规定的所有方法/属性的时候才被认为隐式地实现了此接口。

  如果这东西走起来像鸭子,晃起来像鸭子,那么这就是鸭子!(James Riley)

  那么这个和Structural Subtyping有什么区别?我承认structural subtyping更适合C#的静态样式,所以这是个'static duck typing',或者如wikipedia所述:

  Duck typing与structural typing的区别仅在于类型中被访问的部分在运行期才做兼容性确认。

  我们将通过一个用例来说明这种方法有什么好处:

  在.NET框架中,一部分控件实现了一个叫ReadOnly的属性,比如TextBox, DataGrid, NumericUpDown

  现在我们建一个叫IReadOnlyRestricable的接口

public interface IReadOnlyRestricable
  {
  bool ReadOnly { get; set; }
  }

  然后我们要遍历所有的控件,找出有ReadOnly属性的控件并把此属性设为true(译者注:这些控件本身没有实现IReadOnlyRestricable),在ducktyping下我们可以把控件通过类型转换为IReadOnlyRestricable,就像下面代码一样,这样我们就不需要通过反射去定位ReadOnly属性了:

foreach (Control c in f.Controls)
  {
  //would like to have implicit cast to IReadOnlyRestrictable if interface contract is in class we are checking against
  IReadOnlyRestricable editable = c as IReadOnlyRestricable;
  if (editable != null)
  editable.ReadOnly = true;
  }

  在我看来ducktyping的最大好处是可以为你不需要访问的类库定义一些接口,这可以尽可能地减少相互依赖,你可以查看Phil Haacks more extensive post on duck typing这文章来看看为什么作者相信这对C#有好处。

  Krzysztof Cwalina认为,很显然的,C#的foreach关键字已经使用了duck typing.

  4. 安全的null延迟赋值操作符

  我很想看到一种安全地访问一个值为null的对象的属性的表达式,表达式可能形如Object.Property.Property.Value

  比如我要访问Customer?.FirstName,但是Customer是null,此时Customer?.FirstName会返回null而不是抛出个NullReferenceException

  再看看下面的代码,你就会知道怎么用了:

//FAUX CODE
  //this would throw a null reference exception as usual if either Customer or Order was null
  int orderNumber = Customer.Order.OrderNumber;
  //this would not compile since it would require a nullable return type
  int orderNumber = Customer.Order?.OrderNumber;
  //this would return null if a Customer was null or if Order was null
  int? orderNumber = Customer?.Order?.OrderNumber;
  if (orderNumber.HasValue)
  //... do something with it
  //instead of having to do
  if ((Customer != null) && (Customer.Order != null))
  int a = Customer.Order.OrderNumber

 

  

 

 

  

 

 

相关搜索:
关注此文读者还看过
热门关注
特别推荐
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众