Last post Apr 27, 2015 12:42 PM by PatriceSc
Apr 23, 2015 02:25 PM|Xequence|LINK
What is the purpose of using Stream and why is this an unsafe method?
/// WARNING! This is unsafe method. MemoryStream used inside. should not be used if possible.
/// <param name="inputStream"></param>
public static byte Decompress(Stream inputStream)
using (MemoryStream output = new MemoryStream())
Apr 24, 2015 04:34 AM|PatriceSc|LINK
A Stream is just an "abstraction" for a series of byte (and it could be a network, a file, a memory Stream etc... allowing to use them all the same way).
In my opinion, the comment is just wrong. This is not unsafe with the C# meaning see
https://msdn.microsoft.com/en-us/library/t2yzs44b.aspx and not even otherwise. Likely some confusion (or was afraid about OutOfMemory exceptions etc...)
Apr 24, 2015 04:38 AM|Mikesdotnetting|LINK
why is this an unsafe method?
Apr 24, 2015 11:59 AM|Xequence|LINK
I inherited some code and I am optimizing it and someone put in a startling comment...
Is it bad practice to pass around a stream instead of something else??
Apr 24, 2015 12:04 PM|PatriceSc|LINK
No as I said the comment is IMO just wrong. Ignore it. Only the author could tell us why he wrote this....
Apr 24, 2015 12:34 PM|Xequence|LINK
CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(binaryStorageId);
using (var memoryStream = new MemoryStream())
//var text = Encoding.UTF8.GetString(memoryStream.ToArray());
var base64string = Convert.ToBase64String(memoryStream.ToArray(), 0, memoryStream.ToArray().Length);
I am using another memory stream, can you tell me if this is bad practice to return inside of a using of memory stream?
Apr 27, 2015 12:42 PM|PatriceSc|LINK
It's best to start a new thread rather than to ask a new question in a thread which is already closed.
It's fine. The point is precisely to guarantee the Dispose call even if you have a return Inside the using block (behind the scene it is a try/finally construct).