Skip to content

Instantly share code, notes, and snippets.

@masaab
Last active April 19, 2020 05:57
Show Gist options
  • Select an option

  • Save masaab/28664715fe3a1ccc25a6398226a30085 to your computer and use it in GitHub Desktop.

Select an option

Save masaab/28664715fe3a1ccc25a6398226a30085 to your computer and use it in GitHub Desktop.
Creating bread in restaurant
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
private const int _numberofBreads = 10;
static async Task Main(string[] args)
{
Console.WriteLine("Start Calculating Breads in India and Yemen!");
Restaurant restaurant = new Restaurant();
IEnumerable<int> breadFromIndia = await restaurant.GetBreadIndianWayAsync(_numberofBreads);
IAsyncEnumerable<int> breadsFromYemen = restaurant.GetBreadYemeniWayAsync(_numberofBreads);
foreach (var item in breadFromIndia)
{
Console.WriteLine($"{item}: Indian Bread Ready To serve");
}
await foreach (var item in breadsFromYemen)
{
Console.WriteLine($"{item}: Indian Bread Ready To serve");
}
}
}
public class Restaurant
{
public async Task<IEnumerable<int>> GetBreadIndianWayAsync(int numberOfBreads)
{
List<int> breadList = new List<int>();
for (int i = 1; i <= numberOfBreads; i++)
{
Console.WriteLine($"Baking Indian Bread: {i}");
await Task.Delay(1000);
breadList.Add(i);
}
return breadList;
}
public async IAsyncEnumerable<int> GetBreadYemeniWayAsync(int numberOfBreads)
{
for (int i = 1; i <= numberOfBreads; i++)
{
Console.WriteLine($"Baking Yemeni Bread:{i}");
await Task.Delay(1000);
yield return i;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment