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 the advantage of .net?

724


What is a strong name in .net?

782


How many namespaces are in .net version 1.1?

813


Using activex control in .net?

738


State the various features present in .NET?

787


Do I have any control over the garbage collection algorithm?

765


How to create properties and methods using controls?

776


What is garbage collection and how it works ?

795


What does jit do?

763


What is the difference between function and stored procedure?

769


What is static constructor, when it will be fired?

722


What is a web service in .net?

778


What are the advantages of .net core?

707


Difference between dispose and finallize method?

893


Explain how garbage collection works?

768