Deferred execution in C#

前端 未结 6 2029
无人共我
无人共我 2021-01-04 07:36

How could I implement my own deferred execution mechanism in C#?

So for instance I have:

string x = DoFoo();

Is it possible to perf

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 07:54

    While it's somewhat dirty you could always use the yield keyword:

    public IEnumerable DoFoo() {
       Console.WriteLine("doing foo");
       yield return 10;
    }
    
    [Test]
    public void TestMethod()
    {
        var x = DoFoo();
        Console.WriteLine("foo aquired?");
        Console.WriteLine(x.First());
    }
    

提交回复
热议问题