gpt4 book ai didi

c# - 为什么 'return' 和 'yield return' 在此示例中具有相同的行为?

转载 作者:行者123 更新时间:2023-11-30 16:10:14 28 4
gpt4 key购买 nike

我正在读这个article about fluent-style syntax我试过这个例子。我注意到,当我将 YoungDogs()HerdingDogs() 方法主体转换为 LINQ 表达式时,yield return 被替换为 return ,但该方法的行为保持不变。

为什么将方法更改为 LINQ 表达式会改变数据返回方式的行为?

这是文章中概述的原始示例:

    public static IEnumerable<Dog> YoungDogs(this IEnumerable<Dog> dogs)
{
foreach (Dog d in dogs)
if (d.Age < 10)
yield return d;
}

public static IEnumerable<Dog> HerdingDogs(this IEnumerable<Dog> dogs)
{
foreach (Dog d in dogs)
if ((d.Breed == Breed.BorderCollie) ||
(d.Breed == Breed.Collie) ||
(d.Breed == Breed.Sheltie))
yield return d;
}

这是更改方法后的完整程序:

class Program
{
static void Main(string[] args)
{
foreach (Dog d in AllMyDogs().YoungDogs().HerdingDogs())
{
Console.WriteLine(d.ToString());
if (d.Breed == Breed.JackRussell)
break;
}

Console.ReadLine();
}

private static IEnumerable<Dog> AllMyDogs()
{
yield return new Dog("Kirby", Breed.BorderCollie, 14);
yield return new Dog("Jack", Breed.JackRussell, 15);
yield return new Dog("Ruby", Breed.Mutt, 4);
yield return new Dog("Lassie", Breed.Collie, 19);
yield return new Dog("Shep", Breed.Collie, 2);
yield return new Dog("Foofoo", Breed.Sheltie, 8);
yield return new Dog("Pongo", Breed.Dalmatian, 4);
yield return new Dog("Rooster", Breed.WestHighlandTerrier, 1);
}
}

static class DogFilters
{
public static IEnumerable<Dog> YoungDogs(this IEnumerable<Dog> dogs)
{
return dogs.Where(d => d.Age < 10);
}

public static IEnumerable<Dog> HerdingDogs(this IEnumerable<Dog> dogs)
{
return dogs.Where(d => (d.Breed == Breed.BorderCollie) ||
(d.Breed == Breed.Collie) ||
(d.Breed == Breed.Sheltie));
}
}

public enum Breed
{
BorderCollie,
Collie,
Sheltie,
JackRussell,
Mutt,
Dalmatian,
WestHighlandTerrier
}

public class Dog
{
public string Name { get; set; }
public Breed Breed { get; set; }
public int Age { get; set; }

public Dog(string name, Breed breed, int age)
{
Name = name;
Breed = breed;
Age = age;
}

public bool TryBark(out string barkSound)
{
bool success = false;
barkSound = "";

if (Age <= 10)
{
success = true;
barkSound = "Woof";
}

return success;
}

public string Bark()
{
string barkSound;

if (!TryBark(out barkSound))
throw new Exception("This dog can't bark");
return barkSound;
}

public override string ToString()
{
return string.Format("{0} <{1}>, age {2}", Name, Breed.ToString(), Age);
}
}

最佳答案

dogs.Where(d => d.Age < 10);声明一个序列,当YoungDogs将被调用将被返回。将返回的是序列的声明,而不是“通过你的过滤器”的狗。您将从 dogs 中获得实际对象收集,仅当您要求时。

引擎盖下Where封装对 yield return 的调用通过它 dogs 中的项目通过谓词的一个接一个返回。这发生在下面的实例中:

// That declares the sequence, you want to get. 
// In dogsWithAgeLessThanTen you don't have actaully any dog.
var dogsWithAgeLessThanTen = dogs.Where(d => d.Age < 10);

// Now you request the above query to be executed, that's called lazy loading.
// Here you get one after the other the dogs that have age less than 10.
// The Enumerator you get from the dogsWithAgeLessThanTen returns your data
// through a yield return statement.
foreach(var dog in dogsWithAgeLessThanTen)
{

}

上面是一样的,关于返回的结果如下:

public static IEnumerable<Dog> YoungDogs(this IEnumerable<Dog> dogs)
{
foreach (Dog d in dogs)
if (d.Age < 10)
yield return d;
}

var dogsWithAgeLessThanTen = dogs.YoungDogs();

foreach(var dog in dogsWithAgeLessThanTen)
{

}

关于c# - 为什么 'return' 和 'yield return' 在此示例中具有相同的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178220/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com