c# yield usage

 

  • 返回类型必须为 IEnumerable、IEnumerable<T>、IEnumerator 或 IEnumerator<T>。
    
    声明不能有任何 in、ref 或 out 参数。
    
    
    public class PowersOf2
    {
        static void Main()
        {
            // Display powers of 2 up to the exponent of 8:
            foreach (int i in Power(2, 8))
            {
                Console.Write("{0} ", i);
            }
        }
    
        public static System.Collections.Generic.IEnumerable<int> Power(int number, int exponent)
        {
            int result = 1;
    
            for (int i = 0; i < exponent; i++)
            {
                result = result * number;
                yield return result;
            }
        }
    
        // Output: 2 4 8 16 32 64 128 256
    }

     

 

 

Guess you like

Origin blog.csdn.net/jiuzhouhi/article/details/85627545