Feb 29, 2008

FizzBuzz Object (C#)


class Program
{
static void Main(string[] args)
{
var fizzbuzz = new
{
Case = new[] {
new {
Cond = new Func<int, bool>(n => true),
Proc = new Func<int, string>(n => null)
}
},
Convert = new Func<int, string>(n => null),
Get = new Func<int, int, IEnumerable<string>>((start, count) => null)
};

fizzbuzz = new
{
Case = new[]
{
new {
Cond = new Func<int, bool>(n => n % 15 == 0),
Proc = new Func<int, string>(n => "FizzBuzz")
},
new {
Cond = new Func<int, bool>(n => n % 3 == 0),
Proc = new Func<int, string>(n => "Fizz")
},
new {
Cond = new Func<int, bool>(n => n % 5 == 0),
Proc = new Func<int, string>(n => "Buzz")
},
new {
Cond = new Func<int, bool>(n => true),
Proc = new Func<int, string>(n => n.ToString())
}
},
Convert = new Func<int, string>(n => fizzbuzz.Case.Where(c => c.Cond(n)).First().Proc(n)),
Get = new Func<int, int, IEnumerable<string>>((start, count) => Enumerable.Range(start, count).Select(n => fizzbuzz.Convert(n)))
};

foreach (var x in fizzbuzz.Get(1, 100))
{
Console.Write(x + " ");
}

Console.ReadLine();
}
}



  • It's impossible to access fizzbuzz while initializing of fizzbuzz.

  • It's regarded as same type when an anonymous types have same properties.

  • It's impossible to set lambda expression to a property of anonymous type.

  • It's impossible to use yield keyword in anonymous method.

No comments: