what is yield keyword in .Net?
Answers were Sorted based on User's Feedback
Answer / banaja
public class ExampleIterator : IEnumerable {
public IEnumerator GetEnumerator() {
yield return 1;
}
}
The class ExampleIterator implements the IEnumerable
interface, which requires the GetEnumerator method to be
implemented. The GetEnumerator method returns an
IEnumerator instance. In the implementation of
GetEnumerator, the value 1 is returned rather than an
IEnumerator interface instance. This is odd, because how
can a value type be returned when a reference type is
expected? The magic is the yield keyword, which provides
the missing code in the form of generated IL.
The yield keyword is a compiler directive that generates a
very large chunk of IL code. Using ILDASM.exe it is
possible to reverse engineer what the compiler generated
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / puneet meena
while(get_next_record_from_database)
{ yield return your_next_record;}
It allows you to quickly create an object collection (an Enumerator) that you can loop through and retrieve records. The yield return statement handles all the of the code needed to create an enumerator for you.
The big part of the yield return statement is that you don't have to load all the of the items in a collection before returning the collection to the calling method. It allows lazy loading of the collection, so you don't pay the access penalty all at once.
| Is This Answer Correct ? | 0 Yes | 0 No |
What are Sessions?
i had attended to infosys interview on 17th april 2010...on .net..3+ experience for Technology Analyst .. to my knowledge i did well in technical and hr whether i loose the interview or still processing is taking place..am confused please. what accuatly would be happend?
What is Active Directory? What is the namespace used to access the Microsoft Active Directories? What are ADSI Directories?
What is the use of system.diagnostics.process class in .net?
Is .net is platform independent. If am using solaris, installing .net first u had to install framework, so framework is .exe file then it will not take in solaris or any other than windows,then how .net is platform independent.
What are use of Cloud computing....?
What are three common acronyms used in .net, and what do they stand for?
What is an exe and a dll?
How ccw (com callable wrapper) and rcw (runtime callable wrappers) works?
explain the states of a window service application?
Explain what is the difference between constants and read-only variables?
What is stored procedure ? how we use it in .NET ?