3.days.from_now
Railsは、3 日先の日付を期待するように、戻り値のように Ruby にいくつかのコア拡張機能を導入しました。C# の拡張メソッドを使用して、同様のことができるようになりました。
static class Extensions
{
public static TimeSpan Days(this int i)
{
return new TimeSpan(i, 0, 0, 0, 0);
}
public static DateTime FromNow(this TimeSpan ts)
{
return DateTime.Now.Add(ts);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(
3.Days().FromNow()
);
}
}
またはどうですか:
static class Extensions
{
public static IEnumerable<int> To(this int from, int to)
{
return Enumerable.Range(from, to - from + 1);
}
}
class Program
{
static void Main(string[] args)
{
foreach (var i in 10.To(20))
{
Console.WriteLine(i);
}
}
}
これは根本的に間違っているのでしょうか、それとも、Rails のようなフレームワークのように、それが良い考えである場合もありますか?