title: Is it safe to return a FileStream in a method in C#?
body: <p>In ASP.NET WebForms 4.5, I have WebAPI Controller with a GET method for getting a PDF.</p>
  
  <p>Then down in the business layer of the application, I have an API class with a method that contains the logic for actually finding and returning the PDF to the controller.</p>
  
  <p>So MyController class basically has:</p>
  
  <pre><code>public HttpResponseMessage GetStatement(string acctNumber, string stmtDate) {
      MyApi myApi = new MyApi();
      HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
      FileStream stream = myApi.GetStatement(acctNumber, stmtDate);
      ...set the response.Content = stream...
      ... set the mime type..
      ... close the stream...
      return response;
  }
  </code></pre>
  
  <p>And MyApi class has:</p>
  
  <pre><code>public FileStream GetStatement(string acctNumber, string stmtDate) {
      ... makes an HttpWebRequest to get a PDF from another system ...
      HttpWebRequest req = WebRequest.Create(......)....
      FileStream stream = new FileStream(accountNumber +"_" + stmtDate + ".pdf", FileMode.Create);
      response.GetResponseStream().CopyTo(stream);
      return stream;
  }
  </code></pre>
  
  <p>The API class is not in the web layer of the application because it's used by other (non-web) parts of the software.</p>
  
  <p>I guess my concern is there's no explicit closing of the FileStream in the API method.  I could do it in the Controller method, but I'd be relying on others to do the same when they're calling it from other areas.</p>
  
  <p>Is there a better way to return the PDF file from the API method?  Possibly just as a byte array or something like that?  Preferably as little overhead as possible.</p>
  
  <p>Thanks-</p>