---
title: "ruby 和 c# 3.0"
author: "nacu"
date: "2007-07-02T22:28:41Z"
tags: []
slug: "ruby-c-3-0"
draft: false
lastmod: "2007-07-02T22:28:41Z"
id: 96998
url: "https://nacu.iteye.com/blog/96998"
---

看了C# 3.0 的feature, 瀑布汗了一下，MS 开始抄Ruby了么  
有下例为证  
c# 3.0

c# 代码

 

1. 10.Times((int i) =&gt;
2. {
3. if (i &gt; 5)
4. Console.WriteLine(i);
5. else
6. Console.WriteLine("less than five");
7. }
8. );

ruby:

ruby 代码

 

1. 10.times {|i| puts i.to\_s}

还有

c# 代码

 

1. var dt = 1.Years().Ago();
2. var dt2 = 1.Minutes().Ago();
3. Console.WriteLine("one year age:" + dt.ToShortDateString());
4. Console.WriteLine("one minute age:" + dt2.ToString());

具体实现如下:

c# 代码

 

01. public static class NumericExtention
02. {
03. public static int Years(this int value)
04. {
05. return value * 60 * 60  * 24 \*365;
06. }
07. public static int Seconds(this int value)
08. {
09. return value;
10. }
11. public static int Minutes(this int value)
12. {
13. return value * 60;
14. }
15. public static int Hours(this int value)
16. {
17. return value * 60 * 60;
18. }
19. public static DateTime Ago(this int value)
20. {
21. return DateTime.Now.AddSeconds(-value);
22. }
23. public static void Times(this int value, Action&lt;int&gt; action)
24. {
25. for (int i = 0; i &lt; value; i++)
26. {
27. action(i);
28. }
29. }
30. }