static void Main(string[] args) { var myDB = new Database(); Optional <User> result = myDB.GetUser("John"); if (result.IsPresent()) { Console.WriteLine("Found John!"); } else { Console.WriteLine("Oh no we didn't find him! Let us do something else!"); } result = myDB.GetUser("Jim"); result.IfPresent( x => Console.WriteLine("Found him! Feel free to return things here too!") ); var theObject = myDB.GetUser("James").OrElse(new User("Sophie")); Console.WriteLine(theObject.Name); Optionals.Of(new User("blah")); }
static void Main(string[] args) { Optional <int> emptyOpt = Optionals.Empty <int>(); int result = emptyOpt.IfPresent( x => { return(x + 1); } ); assert(result == default(int)); Optional <int> fullOpt = Optional <int> .Of(32); result = fullOpt.IfPresent(x => { return(x + 1); }); assert(result == 33); }
public Optional <User> GetUser(String name) { User user = db.Find(x => x.Name == name); return(user == null?Optionals.Empty <User>() : Optionals.Of(user)); }