Created
January 21, 2011 07:03
-
-
Save tsukkee/789344 to your computer and use it in GitHub Desktop.
C#'s ternery operator needs left hand side?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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