C# 3.0中类似Ruby的语法
发表于:2007年05月27日  分类:技术  16,919 条评论  2,186 次浏览 

Java:

  1. new Date(new Date().getTime() - 20 * 60 * 1000);

Ruby:

  1. 20.minutes.ago

c#3.0的话有Extension Methods

  1. public static class Extenders
  2. {
  3.   public static DateTime Ago(this TimeSpan val)
  4.   {
  5.     return DateTime.Now.Subtract(val);
  6.   }
  7.  
  8.   public static TimeSpan Minutes(this int val)
  9.   {
  10.     return new TimeSpan(0, val, 0);
  11.   }
  12. }

执行函数

  1. class Program
  2. {
  3.   static void Main(string[] args)
  4.   {
  5.     Console.WriteLine(20.Minutes().Ago());
  6.     Console.ReadLine();
  7.   }
  8. }

原文:http://haacked.com/archive/2007/05/24/ruby-like-syntax-in-c-3.0.aspx
Extension Methods:
New “Orcas” Language Feature: Extension Methods

固定链接: http://liubin.nanshapo.com/2007/05/27/ruby-like-expressiveness-in-c-30/ | 其实我是一个程序员

Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 90 bytes) in /home/liubin/liubin.nanshapo.com/wp-includes/cache.php on line 429