title: Refactoring a library to be async, how can I avoid repeating myself?
body: <p>I have a method like so:</p>
  
  <pre><code>    public void Encrypt(IFile file)
      {
          if (file == null)
              throw new ArgumentNullException(nameof(file));
  
          string tempFilename = GetFilename(file);
          using (var stream = new FileStream(tempFilename, FileMode.OpenOrCreate))
          {
              this.EncryptToStream(file, stream);
              file.Write(stream);
          }
  
          File.Delete(tempFilename);
      }
  </code></pre>
  
  <p>However, I'm wanting to write another method, very similar but it calls WriteAsync instead, such as:</p>
  
  <pre><code>    public async Task EncryptAsync(IFile file)
      {
          if (file == null)
              throw new ArgumentNullException(nameof(file));
  
          string tempFilename = GetFilename(file);
          using (var stream = new FileStream(tempFilename, FileMode.OpenOrCreate))
          {
              this.EncryptToStream(file, stream);
              await file.WriteAsync(stream);
          }
  
          File.Delete(tempFilename);
      }
  </code></pre>
  
  <p>However I don't like having two methods with practically duplicate code. How can I avoid this? The right approach feels like I should use an Action/Delegate, but the signatures are different....</p>
  
  <p>Thoughts?</p>