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

What is a variable of implicit type and what is its scope?

591


What is finalize method in .net?

715


What is shared and repeatable inheritance?

662


Is there any thread in our .net programs?

702


Explain the code behind wors and contrast that using the inline style.

647






Explain difference between panel and groupbox classes using .net?

615


What is close method? How its different from finalize and dispose?

619


Describe the use of following com+ services jit activation, queued components, object pooling.?

618


What is iis? Have you used it?

657


Does .net supports destructors concept?

1725


How to create properties and methods using controls?

666


Tell us why do we use the “using” statement?

632


What is Full trust permission set in .Net

2000


Please explain when should you use .net web forms over asp.net mvc?

638


How does u handle this COM components developed in other programming languages in .NET?

1767