当前位置: 首页>>代码示例>>C#>>正文


C# Func.ToOptionFunc方法代码示例

本文整理汇总了C#中Func.ToOptionFunc方法的典型用法代码示例。如果您正苦于以下问题:C# Func.ToOptionFunc方法的具体用法?C# Func.ToOptionFunc怎么用?C# Func.ToOptionFunc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Func的用法示例。


在下文中一共展示了Func.ToOptionFunc方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ToOptionFunc

        public void ToOptionFunc()
        {
            var f1_0 = new Func<string>(() => "");
            var g1_0 = f1_0.ToOptionFunc();
            Assert.That(g1_0(), Is.EqualTo(Option.Some("")));
            f1_0 = new Func<string>(() => { throw new Exception("oops!"); });
            g1_0 = f1_0.ToOptionFunc();
            Assert.That(g1_0(), Is.EqualTo(Option<string>.None));
            f1_0 = new Func<string>(() => null);
            g1_0 = f1_0.ToOptionFunc();
            Assert.That(g1_0(), Is.EqualTo(Option<string>.None));

            var f2_0 = new Func<int?>(() =>  + 0);
            var g2_0 = f2_0.ToOptionFunc();
            Assert.That(g2_0(), Is.EqualTo(Option.Some( + 0)));
            f2_0 = new Func<int?>(() => { throw new Exception("oops!"); });
            g2_0 = f2_0.ToOptionFunc();
            Assert.That(g2_0(), Is.EqualTo(Option<int>.None));
            f2_0 = new Func<int?>(() => null);
            g2_0 = f2_0.ToOptionFunc();
            Assert.That(g2_0(), Is.EqualTo(Option<int>.None));

            var f1_1 = new Func<string, string>((a1) => a1);
            var g1_1 = f1_1.ToOptionFunc();
            Assert.That(g1_1("1"), Is.EqualTo(Option.Some("1")));
            f1_1 = new Func<string, string>((a1) => { throw new Exception("oops!"); });
            g1_1 = f1_1.ToOptionFunc();
            Assert.That(g1_1("1"), Is.EqualTo(Option<string>.None));
            f1_1 = new Func<string, string>((a1) => null);
            g1_1 = f1_1.ToOptionFunc();
            Assert.That(g1_1("1"), Is.EqualTo(Option<string>.None));

            var f2_1 = new Func<int?, int?>((a1) => a1 + 0);
            var g2_1 = f2_1.ToOptionFunc();
            Assert.That(g2_1(1), Is.EqualTo(Option.Some(1 + 0)));
            f2_1 = new Func<int?, int?>((a1) => { throw new Exception("oops!"); });
            g2_1 = f2_1.ToOptionFunc();
            Assert.That(g2_1(1), Is.EqualTo(Option<int>.None));
            f2_1 = new Func<int?, int?>((a1) => null);
            g2_1 = f2_1.ToOptionFunc();
            Assert.That(g2_1(1), Is.EqualTo(Option<int>.None));

            var f1_2 = new Func<string, string, string>((a1, a2) => a1 + a2);
            var g1_2 = f1_2.ToOptionFunc();
            Assert.That(g1_2("1", "2"), Is.EqualTo(Option.Some("12")));
            f1_2 = new Func<string, string, string>((a1, a2) => { throw new Exception("oops!"); });
            g1_2 = f1_2.ToOptionFunc();
            Assert.That(g1_2("1", "2"), Is.EqualTo(Option<string>.None));
            f1_2 = new Func<string, string, string>((a1, a2) => null);
            g1_2 = f1_2.ToOptionFunc();
            Assert.That(g1_2("1", "2"), Is.EqualTo(Option<string>.None));

            var f2_2 = new Func<int?, int?, int?>((a1, a2) => a1 + a2 + 0);
            var g2_2 = f2_2.ToOptionFunc();
            Assert.That(g2_2(1, 2), Is.EqualTo(Option.Some(1 + 2 + 0)));
            f2_2 = new Func<int?, int?, int?>((a1, a2) => { throw new Exception("oops!"); });
            g2_2 = f2_2.ToOptionFunc();
            Assert.That(g2_2(1, 2), Is.EqualTo(Option<int>.None));
            f2_2 = new Func<int?, int?, int?>((a1, a2) => null);
            g2_2 = f2_2.ToOptionFunc();
            Assert.That(g2_2(1, 2), Is.EqualTo(Option<int>.None));

            var f1_3 = new Func<string, string, string, string>((a1, a2, a3) => a1 + a2 + a3);
            var g1_3 = f1_3.ToOptionFunc();
            Assert.That(g1_3("1", "2", "3"), Is.EqualTo(Option.Some("123")));
            f1_3 = new Func<string, string, string, string>((a1, a2, a3) => { throw new Exception("oops!"); });
            g1_3 = f1_3.ToOptionFunc();
            Assert.That(g1_3("1", "2", "3"), Is.EqualTo(Option<string>.None));
            f1_3 = new Func<string, string, string, string>((a1, a2, a3) => null);
            g1_3 = f1_3.ToOptionFunc();
            Assert.That(g1_3("1", "2", "3"), Is.EqualTo(Option<string>.None));

            var f2_3 = new Func<int?, int?, int?, int?>((a1, a2, a3) => a1 + a2 + a3 + 0);
            var g2_3 = f2_3.ToOptionFunc();
            Assert.That(g2_3(1, 2, 3), Is.EqualTo(Option.Some(1 + 2 + 3 + 0)));
            f2_3 = new Func<int?, int?, int?, int?>((a1, a2, a3) => { throw new Exception("oops!"); });
            g2_3 = f2_3.ToOptionFunc();
            Assert.That(g2_3(1, 2, 3), Is.EqualTo(Option<int>.None));
            f2_3 = new Func<int?, int?, int?, int?>((a1, a2, a3) => null);
            g2_3 = f2_3.ToOptionFunc();
            Assert.That(g2_3(1, 2, 3), Is.EqualTo(Option<int>.None));

            var f1_4 = new Func<string, string, string, string, string>((a1, a2, a3, a4) => a1 + a2 + a3 + a4);
            var g1_4 = f1_4.ToOptionFunc();
            Assert.That(g1_4("1", "2", "3", "4"), Is.EqualTo(Option.Some("1234")));
            f1_4 = new Func<string, string, string, string, string>((a1, a2, a3, a4) => { throw new Exception("oops!"); });
            g1_4 = f1_4.ToOptionFunc();
            Assert.That(g1_4("1", "2", "3", "4"), Is.EqualTo(Option<string>.None));
            f1_4 = new Func<string, string, string, string, string>((a1, a2, a3, a4) => null);
            g1_4 = f1_4.ToOptionFunc();
            Assert.That(g1_4("1", "2", "3", "4"), Is.EqualTo(Option<string>.None));

            var f2_4 = new Func<int?, int?, int?, int?, int?>((a1, a2, a3, a4) => a1 + a2 + a3 + a4 + 0);
            var g2_4 = f2_4.ToOptionFunc();
            Assert.That(g2_4(1, 2, 3, 4), Is.EqualTo(Option.Some(1 + 2 + 3 + 4 + 0)));
            f2_4 = new Func<int?, int?, int?, int?, int?>((a1, a2, a3, a4) => { throw new Exception("oops!"); });
            g2_4 = f2_4.ToOptionFunc();
            Assert.That(g2_4(1, 2, 3, 4), Is.EqualTo(Option<int>.None));
            f2_4 = new Func<int?, int?, int?, int?, int?>((a1, a2, a3, a4) => null);
            g2_4 = f2_4.ToOptionFunc();
//.........这里部分代码省略.........
开发者ID:LangExt,项目名称:LangExt,代码行数:101,代码来源:FuncOptionTest.cs


注:本文中的Func.ToOptionFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。