4

Is it necessary to dispose of both streams, or will the stream reader dispose of the memory stream.

var btyeArray = Encoding.UTF8.GetBytes(DEFAULT_MAPPING_CSV);
using (var memStream = new MemoryStream(btyeArray))
{
    using (var streamReader = new StreamReader(memStream))
    {
        csvParser.Parse<EmployeeImportDTO>(streamReader, configuration);
    }
}

I know to be safe you should call both usings, but are both of them necessary?

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 2
    [Documentation](https://msdn.microsoft.com/en-gb/library/yhfzs7at(v=vs.110).aspx) is a wonderful thing, you know. "The `StreamReader` object calls `Dispose`() on the provided `Stream` object when `StreamReader.Dispose` is called." – Damien_The_Unbeliever Mar 17 '16 at 15:23
  • 1
    Unless you use another constructor that doesn't do the dispose: [StreamReader Constructor (Stream, Encoding, Boolean, Int32, Boolean)](https://msdn.microsoft.com/en-us/library/gg712952(v=vs.110).aspx) The last parameter (set `leaveOpen` to `true`) – xanatos Mar 17 '16 at 15:24
  • Why all the down votes, because the question was so easily answered seems like a valid question? – johnny 5 Mar 17 '16 at 15:26
  • 1
    Links to other similar questions: http://stackoverflow.com/a/3306316/613130, http://stackoverflow.com/questions/1065168/does-disposing-streamreader-close-the-stream (very interesting what Skeet wrote), http://stackoverflow.com/a/3831947/613130 – xanatos Mar 17 '16 at 15:27
  • @xanatos thanks for the help, that question is very similar but this seems a bit more explict, thanks for the link and the parameter. Yeah I guess it is best to be safe then – johnny 5 Mar 17 '16 at 15:29

0 Answers0