Introduction
In this blog, we are discussing the use of the yield keyword with a simple program.
What is the use of the Yield Keyword?
The yield keyword helps us to do custom iteration over the collection. There are two types of iteration.
- Custom Iteration
- Stateful Iteration
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace yieldkeyword
- {
- public class Program
- {
- static List<int> mylist = new List<int>();
- static void addvalues()
- {
- mylist.Add(1);
- mylist.Add(2);
- mylist.Add(3);
- mylist.Add(4);
- mylist.Add(5);
- }
- static void Main(string[] args)
- {
- addvalues();
- foreach(int i in mylist)
- {
- Console.WriteLine(i);
- }
- Console.ReadLine();
- }
- }
- }

Join the conversation! Your thoughts help the community grow.