What Is yield and How Does It Work in C#?

What Is yield and How Does It Work in C#?

C# capabilities keep expanding from year to year. New features enrich software development. However, their advantages may not always be so obvious. For example, the good old yield. To some developers, especially beginners, it's like magic - inexplicable, but intriguing. This article shows how yield works and what this peculiar word hides. Have fun reading!

C## capabilities keep expanding from year to year. New features enrich software development. However, their advantages may not always be so obvious. For example, the good old yield. To some developers, especially beginners, it's like magic - inexplicable, but intriguing. This article shows how yield works and what this peculiar word hides. Have fun reading!

Why You Need yield

The yield keyword is used to build generators of element sequences. These generators do not create collections. Instead, the sequence stores the current state - and moves on to the next state on command. Thus, memory requirements are minimal and do not depend on the number of elements. It's not hard to guess that generated sequences can be infinite.

In the simplest scenario, the generator stores the current element and contains a set of commands that must be executed to get a new element. This is often much more convenient than creating a collection and storing all of its elements.

While there is nothing wrong with writing a class to implement the generator's behavior, yield simplifies creating such generators significantly. You do not have to create new classes - everything works already.

I must point out here that yield is not a feature available exclusively in C#. However, while the concept is the same, in different languages yield may be implemented and used differently. Which is why here's one more reminder that this article talks about yield only in the context of C#.

csharp knowledge

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

GK Quiz for Competitive Exams - Test Your General Knowledge

Attempt this GK Quiz with answers on important events in various months to assess your general knowledge for competitive exams and general knowledge tests.

Knowledge and Reasoning in Artificial Intelligence

A Knowledge Based Agent in Artificial Intelligence has two levels: Knowledge Base (KB) and Inference Engine.

CSharp Fibonacci Series - Write A Fibonacci Program

In this Video you will learn how to write a CSharp Fibonacci Series Programm. Learn how to use the Fibonacci Sequence in C#. Write a little C# Program to get Fibonacci Numbers. This Fibonacci Series is seen in Nature quite often.

Unleashing the Power of Artificial Intelligence to Leverage Knowledge

Artificial intelligence has a crucial role to play in modern businesses. Most companies around the world perceive knowledge management as an IT project. AI in knowledge management allows them to unlock knowledge to improve the decision-making process and generate high ROI.

CSharp Interview 50 Question and Answers

This video is a compilation of my 5 successful videos - 50 C# Interview Questions and Answers and should be seen as interview preparation.