Ruby 语言 思想驱动生活

May 27, 2007

C# 3.0中类似Ruby的语法

Filed under: 技术 — liubin @ 11:30

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


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