C#中的yield return 和return有什么区别呀.
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/07/05 17:55:51
C#中的yield return 和return有什么区别呀.
![C#中的yield return 和return有什么区别呀.](/uploads/image/z/8738762-50-2.jpg?t=C%23%E4%B8%AD%E7%9A%84yield+return+%E5%92%8Creturn%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB%E5%91%80.)
在下面的示例中,迭代器块(这里是方法 Power(int number,int power))中使用了 yield 语句.当调用 Power 方法时,它返回一个包含数字幂的可枚举对象.注意 Power 方法的返回类型是 IEnumerable(一种迭代器接口类型).
// yield-example.cs
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number,int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2,8))
{
Console.Write("{0} ",i);
}
}
}
2 4 8 16 32 64 128 256
// yield-example.cs
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number,int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2,8))
{
Console.Write("{0} ",i);
}
}
}
2 4 8 16 32 64 128 256
C#中的yield return 和return有什么区别呀.
c#中return 0和return -1的区别
return to home和return home有什么区别?
return (*a)++和return++(*a)有什么区别?
return和return back有何区别
go back 和return 有什么区别
return和refund有什么区别
return replace 有什么区别
请问:return from ; return to 有什么区别?arrive from
return to 和return 直接加有何区别?
C语言中return 0;和return(0); 有什么区别?
return complex(a,b)和return (a,b)有什么区别