Saturday, July 28, 2007

FizzBuzz c# 3.0

I've been playing with the C# 3.0 Express beta....

(from n in Enumerable.Range(1, 100)
where (n % 3) == 0 || (n % 2) == 0
select n.ToString() + "=" + (((n % 2) == 0) ? "fizz" : "") + (((n % 3) == 0) ? "buzz" : "")).ToList().ForEach(Console.WriteLine);


3 comments:

James Curran said...

That's pretty cool, but that's not the way I play FizzBuzz:

(from n in Enumerable.Range(1, 100)
select (((n % 3) == 0) ? "fizz" : "")
+ (((n % 5) == 0) ? "buzz" : "")
+ (((n % 3) != 0) && ((n % 5) != 0) ? n.ToString():""))
.ToList().ForEach(Console.WriteLine);

Anonymous said...

finally a chance to write a truly useful extension method...

(from n in Enumerable.Range(1, 100) select n.fizzBuzz()).ToList().ForEach(Console.WriteLine);

public static class FizzExtensions
{
public static string fizzBuzz(this int n)
{
return (((n % 3) == 0) ? "fizz" : "")
+ (((n % 5) == 0) ? "buzz" : "")
+ (((n % 3) != 0) && ((n % 5) != 0) ? n.ToString():"");
}
}

D. Mark Lindell said...

Yes, I messed up with the rules and the math... man I suck at details.