本文主要是介绍【C#】yield使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
💻代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace ConsoleApp15
{internal class Program{static void Main(string[] args){IEnumerable<int> fibonaccis = Fibonacci(10);foreach (var f in fibonaccis){Console.Write("{0} ", f);}IEnumerable<int> fibonaccis1 = FibonacciYield(10);foreach (var f in fibonaccis1){Console.Write("{0} ", f);}Console.ReadLine();}public static List<int> Fibonacci(int count){int p = 1;int c = 1;List<int> result = new List<int>();for (int i = 0; i < count; i++){result.Add(p);Thread.Sleep(500);int temp = p + c;p = c;c = temp;}return result;}public static IEnumerable<int> FibonacciYield(int count){int p = 1;int c = 1;for (int i = 0; i < count; i++){yield return p;Thread.Sleep(500);int temp = p + c;p = c;c = temp;}}}
}
📕参考文献
C#中的yield_c# yield-CSDN博客
这篇关于【C#】yield使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!