[英]How to make a method return IEnumerable<T> from array?
I have a method like below. 我有下面的方法。 Instead of
yield return
I need to store the values in an array and need to return it as IEnumerable. 我需要将值存储在数组中并以IEnumerable的形式返回,而不是
yield return
。 How can I achieve this?..Thanks for your help 我该如何实现?。谢谢您的帮助
public static IEnumerable<UserData> DetailsFactory
{
get
{
yield return new UserData(
"Jim",
"Parker",
"Sheryl")
.SetName("User-1 Address details") as UserData;
}
}
A T[]
is already an IEnumerable<T>
. T[]
已经是IEnumerable<T>
。 So you can just return an array directly: 因此,您可以直接返回一个数组:
return new[] {
new UserData(
"Jim",
"Parker",
"Sheryl")
.SetName("User-1 Address details")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.