First we try, then we trust

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在TechEd2005中学习C# 2.0新特性时看到??运算符,表示从左向右取第一个不为空的值。这让我联想到Ruby中“||=”运算符的使用:

session[:cart] ||= Cart.new

This method is fairly tricky. It uses Ruby’s conditional assignment operator, ||=. If the session hash has a value corresponding to the key :cart, that value is returned immediately. Otherwise a new cart object is created and assigned to the session. This new cart is then returned.(该段内容引自《Agile Web Development with Rails》一书)

感觉C#2.0中的??运算符很象Ruby的这种用法,但仍然需要进一步改进。刚刚在虚拟机器上装了.net 2005和SQL 2005,有时间深入挖掘一下C# 2.0。

在idior的一篇文章中还曾经讲过C# 2.0模拟Closures的问题,这里不在重复了。等回去后再考证一番。

posted on 2005-09-24 17:42  吕震宇  阅读(1798)  评论(1编辑  收藏  举报