C# 3.0中类似Ruby的语法
Java:
- new Date(new Date().getTime() - 20 * 60 * 1000);
Ruby:
- 20.minutes.ago
c#3.0的话有Extension Methods
- public static class Extenders
- {
- public static DateTime Ago(this TimeSpan val)
- {
- return DateTime.Now.Subtract(val);
- }
- public static TimeSpan Minutes(this int val)
- {
- return new TimeSpan(0, val, 0);
- }
- }
执行函数
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(20.Minutes().Ago());
- Console.ReadLine();
- }
- }
原文:http://haacked.com/archive/2007/05/24/ruby-like-syntax-in-c-3.0.aspx
Extension Methods:
New “Orcas” Language Feature: Extension Methods
Filed under: 技术 —
Comments (16919)