After reading various posts and SO questions, I understand that If we have very long chain of data then going with yield can be good but please tell me how it is proficient as far as ASP.NET or you can say disconnected web architecture point of view. For example I am bringing my data from database through DAL and now the connection is disconnected . Now I have data in my BL from my DAL now I have to apply foreach for that data (I need to process each of them one by one) and return the collection then to my UI . Will you think it will be good to use yield here ?
Thanks
Vij
Yield basically instructs the compiler to generate an Enumerator (which is a simple state machine) that streams your data on demand, the foreach loop itearing over the IEnumerable “pulling” each element.
So all that a yield statement can do in your particular context is providing lazy streaming semantics, which means that a receiver can stop iterating any time and therefore reduce the amount of data transferred. Contrast that with returning a completely filled collections in one batch, which is what you get when not using
yield. If thats an efficient thing or not depends on a lot of factors you didn’t give any information about, so I can’t help you any further.