C
C#5mo ago
eid

yield return

i can't understand the benefits of yield return , if it's benefit to avoid fetching all data from database and storing them in the memory,and reading each data on demand. i think i can does that by stream types,so what the need to yield return?
15 Replies
Jimmacle
Jimmacle5mo ago
yield return is for producing sequences one element at a time, that's it it has nothing to do with databases
eid
eidOP5mo ago
and has nothing to do with reading file?
canton7
canton75mo ago
Note that Stream just works with bytes
Jimmacle
Jimmacle5mo ago
no, it is a standalone language feature you can use it with anything
canton7
canton75mo ago
Streams are specifically for reading sets of bytes, from a source which might be able to read 0, 1, or many bytes at that point in time
Jimmacle
Jimmacle5mo ago
public IEnumerable<int> GetSomeNumbers()
{
yield return 1;
yield return 2;
yield return 3;
}
public IEnumerable<int> GetSomeNumbers()
{
yield return 1;
yield return 2;
yield return 3;
}
canton7
canton75mo ago
Iterators are a different mechanism. They work with any element type, and they just yield a single element at a time
Jimmacle
Jimmacle5mo ago
public IEnumerable<int> GetPowersOf2()
{
var current = 1;
while (true)
{
yield return current;
current *= 2;
}
}
public IEnumerable<int> GetPowersOf2()
{
var current = 1;
while (true)
{
yield return current;
current *= 2;
}
}
some examples
eid
eidOP5mo ago
i like the example, but i'm talking about real example
Jimmacle
Jimmacle5mo ago
producing infinite sequences is a real example
eid
eidOP5mo ago
i mean how c# used in in it's built-in language, what's the apply of it in the language
canton7
canton75mo ago
foreach loops for one: the ability to iterate over collections
reflectronic
reflectronic5mo ago
private IEnumerable<T> Where<T>(IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (var item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
private IEnumerable<T> Where<T>(IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (var item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
canton7
canton75mo ago
All of Linq: the ability to filter and transform sequences of elements
SpReeD
SpReeD5mo ago
Also notable is that the result of an IEnumerable<T> function isn't called on it's assignment, but it's usage. Combined with a filter like Where(<predicate>) you don't need to loop over each element. And depending on the condition, not even once. Example:
private IEnumerable<int> GiveMeInts()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}

IEnumerable<int> ints = this.GiveMeInts(); // not called
int[] intsArray = this.GiveMeInts().ToArray(); // called

foreach (int i in ints) // calls GiveMeInts
{
Debug.Print($"{i}");
}
private IEnumerable<int> GiveMeInts()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}

IEnumerable<int> ints = this.GiveMeInts(); // not called
int[] intsArray = this.GiveMeInts().ToArray(); // called

foreach (int i in ints) // calls GiveMeInts
{
Debug.Print($"{i}");
}
It's mostly performance related, also by using an interface the user is free to whatever collection one wanna implement. A win-win situation.
Want results from more Discord servers?
Add your server