---
title: "yield Statement in C#"  
description: "yield Statement in C#"  
author: "Anonymous User"  
published: 2015-11-27  
updated: 2015-11-27  
canonical: https://www.mindstick.com/forum/33651/yield-statement-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 1 minute  

---

# yield Statement in C#

i want to use yield in c# how to use please help me.

## Replies

### Reply by Anonymous User

With yield keyword, the control moves from the caller to source and from the source back to the caller .

yield statement can be used in two forms: yield return <expression>;

yield return statement returns each element at at time.

Yield return allows you to run custom iteration without temp collection.

```
using System;using System.Collections.Generic;namespace RandomNumberApp{    class RandomNumber    {        static void Main(string[] args)        {            foreach (int i in PrintRandomNumbers(10))            {                Console.WriteLine(i);            }            Console.ReadLine();        }        static IEnumerable<int> PrintRandomNumbers(int counter)        {            Random rand = new Random();            for (int i = 0; i < counter; i++)            {                yield                return rand.Next();            }        }    }}
```

```

```


---

Original Source: https://www.mindstick.com/forum/33651/yield-statement-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
