mardi 6 décembre 2016

Trying to makse sense of CA2202: Do not dispose objects multiple times

I am going through the explanation of CA2202 here:

http://ift.tt/2g7nEGV

It specifically asks:

When to Suppress Warnings

Do not suppress a warning from this rule. Even if Dispose for the object is known to be safely callable multiple times, the implementation might change in the future.

That means I really have to fix the issue, even if my code is perfectly working for some time in production and passed all tests. Okay.

My code is very similar to the exmaple below:

using (Stream stream = new FileStream("file.txt", FileMode.OpenOrCreate))  
{  
    using (StreamWriter writer = new StreamWriter(stream))  
    {  
        // Use the writer object...  
    }  
}  

If above code is not correct and it is not the right way of disposing, what would be the correct code to dispose both stream and writer guaranteed?

Aucun commentaire:

Enregistrer un commentaire