Question

I assumed that all Non-Public (ie, private, protected, internal, and internal protected) members of C# objects go under "Non-Public Members" when I look at objects in Visual Studio's Watch Window. But then, I noticed an anamoly with this code:

   class HashDerived : System.Security.Cryptography.HashAlgorithm { ... }

   HashAlgorithm hash1 = new HashDerived();
   HashAlgorithm hash2 = new System.Security.Cryptography.SHA1Cng();

hash1's "Non-Public Members" looks like this:

hash1 in Watch Window

whereas hash2's "Non-Public Members" looks like this:

hash2 in Watch Window

So it seems like for hash1, only the private field (m_bDisposed) appears under the "Non-Public members" node, where for hash2, even protected and protected internal members like "HashSizeValue" and "HashValue" appear in it.

Why does this happen? What are the rules behind this behaviour?

Was it helpful?

Solution

The behavior you are seeing here is a bug. The C# debugger shouldn't be showing static members in this scenario. I confirmed this with the current owner of the code base and he's going to file a bug for the next release of Visual Studio.

The specific scenario under which this happens is

  • Just My Code is enabled
  • The type is defined in what's determined to be a non-user assembly
  • The type of the reference and object instance are different (switch hash2 to SHA1Cnf and the problem disappears)

Note there may be other scenarios where this appears. This is the behavior I was able to narrow down in the debugging / experimentation that I did.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top