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

Related posts for the current post: