Ruby 语言 思想驱动生活

Ruby,Rails,编程是一种乐趣。

Tag List for:

C# 3.0中类似Ruby的语法

Posted by liubin on Sunday May 27 @ 11:30
Taged with: tagged and

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

TAG:


Locations of visitors to this page


Recent Comments