Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created January 21, 2011 07:03
Show Gist options
  • Save tsukkee/789344 to your computer and use it in GitHub Desktop.
Save tsukkee/789344 to your computer and use it in GitHub Desktop.
C#'s ternery operator needs left hand side?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TernaryOperatorTest
{
class Program
{
static void Main(string[] args)
{
string s;
Enumerable.Range(1, 100).Select((i) =>
{
return (s = (i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "")) == "" ? i.ToString() : s;
}).ToList().ForEach(Console.WriteLine);
Console.Read(); // don't close console window
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TernaryOperatorTest
{
static class IntTimes
{
public static void Times(this int len, Action<int> func)
{
for(int i = 1; i <= len; ++i) func(i);
}
}
class Program
{
static bool IsFizz(int i) { return i % 3 == 0; }
static bool IsBuzz(int i) { return i % 5 == 0; }
static int DoFizzBuzz() { Console.WriteLine("FizzBuzz"); return 0; /* dummy */ }
static int DoFizz() { Console.WriteLine("Fizz"); return 0; /* dummy */ }
static int DoBuzz() { Console.WriteLine("Buzz"); return 0; /* dummy */ }
static int Else(int i) { Console.WriteLine(i); return 0; /* dummy */ }
static void Main(string[] args)
{
(100).Times((i) => {
int dummy = IsFizz(i) && IsBuzz(i) ? DoFizzBuzz() :
IsFizz(i) ? DoFizz() :
IsBuzz(i) ? DoBuzz() :
Else(i);
});
Console.Read(); // don't close console window
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment