本文整理汇总了C#中Option.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Option.GetType方法的具体用法?C# Option.GetType怎么用?C# Option.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Option
的用法示例。
在下文中一共展示了Option.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PlayAroundOption
private static void PlayAroundOption()
{
Console.WriteLine("==================== Option ====================");
var thai = Create.Option(Nationality.Thai);
var japanese = Create.Option(Nationality.Japanese);
var nullOption = Create.Option<Nationality>(null);
Console.WriteLine("{0} is {1}", thai, thai.IsSome ? "Some" : "None");
Console.WriteLine("{0} is {1}", japanese, japanese.IsSome ? "Some" : "None");
Console.WriteLine("{0} is None = {1}", nullOption, nullOption.IsNone);
Console.WriteLine("{0} if Thai",Nationality.Thai.ToString().NoneIf("Thai"));
string nullStr = null;
Console.WriteLine("{0} if null", nullStr.NoneIfNull());
Func<string, Nationality> func = a =>
{
switch (a.ToLower())
{
case "thai":
return Nationality.Thai;
case "japanese":
return Nationality.Japanese;
default:
throw new Exception();
}
};
Console.WriteLine(Option.FromFunc(() => func("Thai")));
Console.WriteLine(Option.FromFunc(() => func("THAI")));
Console.WriteLine(Option.FromFunc(() => func("japanese")));
Console.WriteLine(Option.FromFunc(() => func("Chinese")));
var emptyOption = new Option<int>();
Console.WriteLine("{0} is None : {1}", emptyOption.GetType(), emptyOption.IsNone);
var sizes = new[] {"S", "M", "L", "XL", "", null};
foreach (var option in Enumerable.Select(sizes, Create.Option))
{
Console.WriteLine(option.GetOr("F"));
Console.WriteLine(option.GetOrElse(() => "No option ==> Do this instead"));
Console.WriteLine("{0} is Some {1}", option, option.Match(
Some: v => true,
None: () => false));
option.Match(
Some: v => Console.WriteLine("{0} do something", v),
None: () => Console.WriteLine("None do nothing"));
if (option == Option.Some("XL")) Console.WriteLine("You're overweight, do more exercise!!!");
else Console.WriteLine("Skinny!!");
Console.WriteLine(option.Maybe("F", v => string.Format("{0} is a Valid Size", v)));
}
//Fold?
var opt = Option.Some("abcd");
Console.WriteLine("Fold : {0}", opt.Fold("efgh", (a, b) => string.Concat(a, b)));
Console.WriteLine();
}