在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的问题,这里不在重复了。等回去后再考证一番。