Created
September 11, 2017 01:03
-
-
Save phillipsj/24efbde38b7bb947b67c46cdd7caf636 to your computer and use it in GitHub Desktop.
C# Option 1
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; | |
namespace option { | |
class Program { | |
static void Main(string[] args) { | |
string message; | |
var option = GetOption(true); | |
Console.WriteLine(EvaulateOption(option)); | |
Console.ReadLine(); | |
} | |
static Option GetOption(bool ifSome) { | |
return ifSome ? new Some<string>("Hello!") : new None() as Option; | |
} | |
static string EvaulateOption(Option option) { | |
switch (option) { | |
case Some<string> s: | |
return s.Value; | |
case None n: | |
return "None"; | |
default: | |
return "Default"; | |
} | |
} | |
} | |
abstract class Option { | |
} | |
class Some<T> : Option { | |
public T Value { get; } | |
public Some(T value) { | |
Value = value; | |
} | |
} | |
class None : Option { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment