title: C#: Should Streams be closed/disposed in called method?
body: <p>I have a method that I am writing in C# which accepts a string which contains an XML document, and an array of streams that are XSDs.  The string document is validated against the XSDs:</p>
  
  <pre><code>private static XmlValidationResult ValidateDocumentInternal(string document, params Stream[] xsdStreams)
  {
      XmlReaderSettings settings = new XmlReaderSettings
      {
          ValidationType = ValidationType.Schema
      };
  
      foreach (var xsdStream in xsdStreams)
      {
          using (xsdStream)
          {
              XmlReader xmlReader = XmlReader.Create(xsdStream);
  
              try
              {
                  settings.Schemas.Add(null, xmlReader);
              }
              finally
              {
                  xmlReader.Close();
              }
          }
      }
  
      var validationErrors = new List&lt;string&gt;();
  
      settings.ValidationEventHandler += (object sender, System.Xml.Schema.ValidationEventArgs e) =&gt;
      {
          validationErrors.Add($"({e.Exception.LineNumber}): {e.Message}");
      };
  
      using (var stream = document.ToStream())
      {
          var reader = XmlReader.Create(stream, settings);
  
          while (reader.Read())
          {
          }
      }
  
      return new XmlValidationResult
      {
          Success = validationErrors.Count == 0,
          ValidationErrors = validationErrors
      };
  }
  </code></pre>
  
  <p>My question is, should this method be disposing of the XSD streams or should that be the responsibility of the caller?  Imagine the following code which passes in the document and XSD and expects <code>ValidateDocumentInternal</code> to dispose the XSD stream:</p>
  
  <pre><code>var document = GetDocument();
  Stream xsd = GetXSD();
  var validationResult = ValidateDocumentInternal(document, xsd);
  </code></pre>
  
  <p>or should it be like (not disposing of the stream in <code>ValidateDocumentInternal</code>):</p>
  
  <pre><code>var document = GetDocument();
  using (Stream xsd = GetXSD()) {
      var validationResult = = ValidateDocumentInternal(document, xsd);
  }
  </code></pre>
  
  <p>or alternatively should I just pass in a <code>bool</code> saying whether to dispose or not?</p>