what is yield keyword in .Net?

Answers were Sorted based on User's Feedback



what is yield keyword in .Net?..

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

what is yield keyword in .Net?..

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

Post New Answer

More Dot Net General Interview Questions

hai, about trading domain and need simple project on trading system. please help me...

0 Answers  


Differentiate between 'DataSet' and 'SQLDataReader' in ADO.NET?

0 Answers   QuestPond,


what are the controls used to upload a file from client to server?

4 Answers  


What is an Interface? Have you ever developed an Interface.

5 Answers   Deloitte,


What is UDDI and how to register a web service ?

2 Answers   Msoft, TCS,






Explain the difference between the while and for loop. Provide a .net syntax for both loops?

0 Answers  


What is Code group in .Net with respect to CAS

0 Answers  


What is a strong name in .net?

0 Answers  


What are data types in .NET?

0 Answers   UGC Corporation,


What are the advantages of .net core?

0 Answers  


What is Delegate? Have ever used Delegates in your project.

1 Answers   Deloitte,


Hi, Requirement is: try { \\SQL Query } catch(Exception i) { print a } catch(SQLQueryException e) { \\SQL Query } Got Exception in "try" block. Which "catch" throws exception and Why??? Please provide the answer in detail.. Thanks for the help!!!

3 Answers   3i Infotech,


Categories