what is yield keyword in .Net?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain Creational design pattern in .NET?

627


Difference between value type & reference types ?

595


What is jit compilers?

581


Should I use readerwriterlock instead of monitor.enter/exit?

543


What is the difference between vb.net and vc#.net?

553






Is .net a language?

566


Using com component in .net?

606


How does the generational garbage collector in the .net clr manage object lifetime? What is non-deterministic finalization?

562


Is .net front end or backend?

584


Which namespace is require to used XML in .NET?

600


What are the different types of memory in .net?

610


What does msil do?

555


What is the difference between a class and an object, and how do these terms relate to each other?

596


What is concurrency? How will you avoid concurrency when dealing with dataset?

597


What are asp.net authentication providers and iis security?

539