Last active
          February 22, 2022 11:17 
        
      - 
      
 - 
        
Save huanlin/8b456d328971e14c8392cb189aa9c804 to your computer and use it in GitHub Desktop.  
    C# Async Streams
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IAsyncEnumerator<int> e = GetNumbers().GetAsyncEnumerator(); | |
| while (await e.MoveNextAsync()) | |
| { | |
| Console.WriteLine($"取得 {e.Current}"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IEnumerable<int> numbers = GetNumbers(); | |
| foreach (int n in numbers) | |
| { | |
| Console.WriteLine($"取得 {n}"); | |
| } | |
| static IEnumerable<int> GetNumbers() | |
| { | |
| var numbers = new int[] { 1, 2, 3 }; | |
| foreach (int n in numbers) | |
| { | |
| yield return n; | |
| Console.WriteLine($"回傳 {n} 之後"); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IEnumerator<int> e = GetNumbers().GetEnumerator(); | |
| while (e.MoveNext()) | |
| { | |
| Console.WriteLine($"取得 {e.Current}"); | |
| } | |
| // 底下是先前的寫法,一併列出來,方便對照。 | |
| IEnumerable<int> numbers = GetNumbers(); | |
| foreach (int n in numbers) | |
| { | |
| Console.WriteLine($"取得 {n}"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IEnumerable<int> GetNumbers() | |
| { | |
| yield return 1; | |
| Console.WriteLine("回傳 1 之後"); | |
| yield return 2; | |
| Console.WriteLine("回傳 2 之後"); | |
| yield return 3; | |
| Console.WriteLine("回傳 3 之後"); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IEnumerable<int> GetNumbers() | |
| { | |
| var numbers = await SomeApi.GetNumbersAsync(); | |
| foreach (int n in numbers) | |
| { | |
| yield return n; | |
| Console.WriteLine($"回傳 {n} 之後"); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | async IEnumerable<int> GetNumbers() | |
| { | |
| var numbers = await SomeApi.GetNumbersAsync(); | |
| ... (略) | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | async IAsyncIEnumerable<int> GetNumbers() | |
| { | |
| var numbers = await SomeApi.GetNumbersAsync(); | |
| foreach (int n in numbers) | |
| { | |
| yield return n; | |
| Console.WriteLine($"回傳 {n} 之後"); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | IAsyncEnumerable<int> numbers = GetNumbers(); | |
| await foreach (int n in numbers) | |
| { | |
| Console.WriteLine($"取得 {n}"); | |
| } | |
| async IAsyncEnumerable<int> GetNumbers() | |
| { | |
| await Task.Delay(1000); | |
| yield return 1; | |
| await Task.Delay(1000); | |
| yield return 2; | |
| await Task.Delay(1000); | |
| yield return 3; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment