C# yield Example - Dot Net Perls (2024)

HomeC#

yield Example

This page was last reviewed on Jan 16, 2024.

Dot Net Perls

Yield. This C# keyword interacts with the foreach-loop. It is a contextual keyword: yield is a keyword only in certain statements. It can make loops clearer and simpler to write.

C# yield Example - Dot Net Perls (1)

Yield allows each iteration in a foreach-loop to be generated only when needed. In this way (by enabling lazy evaluation) it can improve performance.

An example. We use yield in methods that return the type IEnumerable (without any angle brackets), or the type IEnumerable with a type parameter in the angle brackets.

Info We reference the System.Collections namespace for the first version, and the System.Collections.Generic namespace for the second.

Part 1 In the part of the foreach-loop following the "in" keyword, there is a method call to ComputePower.

foreach

Part 2 ComputePower() receives 2 parameters. It returns an IEnumerable int type, which we can use in a foreach-loop.

C# yield Example - Dot Net Perls (2)

using System;using System.Collections.Generic;public class Program{ static void Main() { // Part 1: compute powers of two with the exponent of 30. foreach (int value in ComputePower(2, 30)) { Console.WriteLine(value); } } public static IEnumerable<int> ComputePower(int number, int exponent) { // Part 2: continue loop until the exponent count is reached. // ... Yield the current result. int exponentNum = 0; int numberResult = 1; while (exponentNum < exponent) { // Multiply the result. numberResult *= number; exponentNum++; // Return the result with yield. yield return numberResult; } }}

248163264128256512102420484096819216384327686553613107226214452428810485762097152419430483886081677721633554432671088641342177282684354565368709121073741824

Internals. The C# code you have that uses yield is not directly executed by the runtime. Instead, the C# compiler transforms that code before the runtime ever occurs.

Tip The compiler-generated class, marked with CompilerGenerated, instead uses several integral types.

Result We see an entire class that is similar to how your code would look if you manually implemented the interfaces.

Info The punctuation characters allow the compiler to ensure no naming conflicts will occur with your code.

// Nested Types[CompilerGenerated]private sealed class <ComputePower>d__0 : IEnumerable<int>, IEnumerable, IEnumerator<int> // ...{ // Fields private int <>1__state; private int <>2__current; public int <>3__exponent; public int <>3__number; private int <>l__initialThreadId; public int <exponentNum>5__1; public int <numberResult>5__2; public int exponent; public int number; // Methods [omitted]}

Benchmark, yield. Is yield return fast? Or does it incur a significant performance loss? Yield return does have some overhead, but if you need its advanced features it can be faster.

Version 1 This version of the code uses a foreach-loop over an array and multiplies each element.

Version 2 Here we use a yield return method (one that returns IEnumerable) to perform the multiplication.

Result The version that uses yield return runs many times slower. For simple things, avoid yield return for top speed.

using System;using System.Collections.Generic;using System.Diagnostics;class Program{ const int _max = 1000000; static void Main() { int[] numbers = { 10, 20, 30, 40, 50 }; var s1 = Stopwatch.StartNew(); // Version 1: use foreach with array. for (int i = 0; i < _max; i++) { if (Method1(numbers) != 300) { throw new Exception(); } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use foreach with yield keyword. for (int i = 0; i < _max; i++) { if (Method2(numbers) != 300) { throw new Exception(); } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } static int Method1(int[] numbers) { // Sum with simple statements. int sum = 0; foreach (int number in numbers) { sum += number * 2; } return sum; } static int Method2(int[] numbers) { // Use yield return to get multiplied numbers and then sum those. int sum = 0; foreach (int number in GetNumbers(numbers)) { sum += number; } return sum; } static IEnumerable<int> GetNumbers(int[] numbers) { // Return all numbers multiplied. foreach (int number in numbers) { yield return number * 2; } }}

3.84 ns inline expression50.68 ns yield return

Notes, IEnumerable. IEnumerable is an interface. It describes a type that implements the internal methods required for using foreach-loops.

IEnumerable

Return. Yield return is similar to a return statement, followed by a "goto" to the yield statement in the next iteration of the foreach-loop.

returngoto

Yield benefits. Suppose we have a large (or even infinite) set of items (like prime numbers, or species of beetles). Yield can provide a benefit here.

Tip We can just loop over items from the infinite set until we encounter one that matches our requirements.

And This reduces memory, reduces loading time of the set, and can also result in cleaner C# code.

Summary. We examined the yield return pattern. The yield keyword is contextual—it only has special significance in some places. It helps simplify foreach-loops.

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Jan 16, 2024 (edit).

HomeChanges

© 2007-2024 Sam Allen.

C# yield Example - Dot Net Perls (2024)
Top Articles
Things AEW Does Better Than WWE
McConnell, Trump bury hatchet with eye on GOP takeover
Tattoo Shops Lansing Il
Cranes For Sale in United States| IronPlanet
Washu Parking
Jackerman Mothers Warmth Part 3
Faint Citrine Lost Ark
Readyset Ochsner.org
³µ¿Â«»ÍÀÇ Ã¢½ÃÀÚ À̸¸±¸ ¸íÀÎ, ¹Ì±¹ Ķ¸®Æ÷´Ï¾Æ ÁøÃâ - ¿ù°£ÆÄ¿öÄÚ¸®¾Æ
Kristine Leahy Spouse
Mlifeinsider Okta
Cvs Devoted Catalog
Gt Transfer Equivalency
Hartford Healthcare Employee Tools
charleston cars & trucks - by owner - craigslist
24 Best Things To Do in Great Yarmouth Norfolk
Bcbs Prefix List Phone Numbers
Define Percosivism
Zalog Forum
Lehmann's Power Equipment
H12 Weidian
Wbiw Weather Watchers
Tips on How to Make Dutch Friends & Cultural Norms
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Rust Belt Revival Auctions
Roane County Arrests Today
Boise Craigslist Cars And Trucks - By Owner
Spiritual Meaning Of Snake Tattoo: Healing And Rebirth!
Sound Of Freedom Showtimes Near Movie Tavern Brookfield Square
Telegram Voyeur
Saxies Lake Worth
Unity Webgl Car Tag
3 Ways to Format a Computer - wikiHow
Possum Exam Fallout 76
Ewg Eucerin
Martins Point Patient Portal
Southtown 101 Menu
Bfri Forum
Indiana Jones 5 Showtimes Near Jamaica Multiplex Cinemas
Manuel Pihakis Obituary
Diana Lolalytics
Scanning the Airwaves
Boone County Sheriff 700 Report
The Conners Season 5 Wiki
Trivago Anaheim California
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Advance Auto.parts Near Me
Hanco*ck County Ms Busted Newspaper
DL381 Delta Air Lines Estado de vuelo Hoy y Historial 2024 | Trip.com
De boeken van Val McDermid op volgorde
Pulpo Yonke Houston Tx
Tweedehands camper te koop - camper occasion kopen
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6422

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.