why do we need Yield Keyword in C#?
what is the exact use of yield keyword in C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Nov 2, 2020, 7:51 AM
now you are asked to create a method to return all numbers greater than 7 , and you are not allowed to use Linq.
so when we are not allowed to use Linq , then creating a method which return some entity out of many , unnecessarily forces us to create another temperory List to hold such values
ex
As you can see , we are unnecessaryliy creating Temperory List.
Yield solves this problem
so , you can see we are not creating temperory List.
How Yield successfully solves the problem?
Ans:- Yield does stateful iteration , meaning it remembers the last location from where he has to iterate everytime.