Click here to Skip to main content
15,901,205 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
Generalc# Const vs. Readonly, Scope, Usings Pin
pherschel7-Jun-17 4:36
pherschel7-Jun-17 4:36 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Slacker0077-Jun-17 4:42
professionalSlacker0077-Jun-17 4:42 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
pherschel7-Jun-17 4:55
pherschel7-Jun-17 4:55 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Slacker0077-Jun-17 5:00
professionalSlacker0077-Jun-17 5:00 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz7-Jun-17 5:34
TheGreatAndPowerfulOz7-Jun-17 5:34 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill7-Jun-17 7:57
professionalFoothill7-Jun-17 7:57 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Slacker0077-Jun-17 8:44
professionalSlacker0077-Jun-17 8:44 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill7-Jun-17 9:20
professionalFoothill7-Jun-17 9:20 
That little path of discovery started when I started learning the Cryptography namespace. This little code snippet (copied directly from MSDN) was getting flagged with CA2202 during code analysis.
C#
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
 {
  using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  {
   using (StreamReader srDecrypt = new StreamReader(csDecrypt))
   {
    // Read the decrypted bytes from the decrypting stream
    // and place them in a string.
    plaintext = srDecrypt.ReadToEnd();
   }
  }
 }

This really boggled me as this is Microsoft example code being flagged as incorrect so I began to dig. Turns out there is still a lively debate about this since the documentation is a little unclear in this area. Refactoring it to the following stopped the code analysis from flagging the code.
C#
MemoryStream memStream = new MemoryStream(data);
CryptoStream decStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);

using (StreamReader reader = new StreamReader(decStream))
{
 decryptedValue = Encoding.UTF8.GetBytes(reader.ReadToEnd());
}

Research into this led me to this one little line in this MSDN article: StreamReader Constructor.
The StreamReader object calls Dispose() on the provided Stream object when StreamReader.Dispose is called.
This reads that when you close certain classes of streams, they also close the streams that underlie them as well.

TL;DR
Not all IDisposable classes behave the same way, event in Microsoft code, and the using statement isn't always the correct way.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Slacker0077-Jun-17 11:24
professionalSlacker0077-Jun-17 11:24 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
carloscs8-Jun-17 7:50
carloscs8-Jun-17 7:50 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill8-Jun-17 8:11
professionalFoothill8-Jun-17 8:11 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
carloscs8-Jun-17 9:04
carloscs8-Jun-17 9:04 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill8-Jun-17 9:41
professionalFoothill8-Jun-17 9:41 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
carloscs8-Jun-17 11:23
carloscs8-Jun-17 11:23 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill8-Jun-17 11:34
professionalFoothill8-Jun-17 11:34 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Mike Marynowski8-Jun-17 10:42
professionalMike Marynowski8-Jun-17 10:42 
GeneralRe: c# Const vs. Readonly, Scope, Usings PinPopular
Dan Neely7-Jun-17 4:51
Dan Neely7-Jun-17 4:51 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Chris Maunder7-Jun-17 4:52
cofounderChris Maunder7-Jun-17 4:52 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
irneb8-Jun-17 5:41
irneb8-Jun-17 5:41 
GeneralRe: c# Const vs. Readonly, Scope, Usings PinPopular
OriginalGriff7-Jun-17 4:52
mveOriginalGriff7-Jun-17 4:52 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
lopatir7-Jun-17 5:41
lopatir7-Jun-17 5:41 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz7-Jun-17 5:43
TheGreatAndPowerfulOz7-Jun-17 5:43 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Fabio Franco8-Jun-17 1:50
professionalFabio Franco8-Jun-17 1:50 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz8-Jun-17 4:48
TheGreatAndPowerfulOz8-Jun-17 4:48 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Fabio Franco8-Jun-17 5:03
professionalFabio Franco8-Jun-17 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   502 votes