4

I was refreshing my memory on C#'s ThreadStaticAttribute this morning, and the following line jumped out at me:

Use this attribute as it is, and do not derive from it.

This line is present in the docs for all versions of the .Net Framework, and Microsoft's own code analysis on the issue states that attributes should only be left unsealed if they are designed to be part of a hierarchy of attributes.

So - the ThreadStaticAttribute class is not now, and does not appear ever to have been, sealed. Why?

Matt Mills
  • 8,692
  • 6
  • 40
  • 64
  • Although this is an interesting question, it could be something as simple as "the developer responsible forgot" to "it would cause the JIT to fail". Let's hope the right person stumbles across this, the potential answerer pool must be small for this one. Subclassing it is pointless, just tried it and it causes the behaviour to fail (as in, the field is no longer thread-static). – Adam Houldsworth Aug 29 '12 at 14:41

1 Answers1

2

There's lots of things that are a bad idea to do 99% of the time, and a great idea 1%.

There are even more things that are a bad idea to do if you're not developing the framework itself, but someone has to in the framework.

In this case, it's more likely that someone may want to add a special case of ThreadStaticAttribute to the framework than it is that you or I will want to.

Even that is probably not that likely, but should they actively prevent it now, or leave the possibility open?

You could argue either way, but someone decided to remain with the possibility. (Or didn't "they just forgot" can't be ruled out).

Note that if they did seal it now, it would be a breaking change should there be anyone out there who did have success with a derived attribute. Even if it's already been argued that they shouldn't do it, and even though almost all changes have some risk of breaking something, it would be an argument in keeping it as-is now.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • "There's lots of things that are a bad idea to do 99% of the time, and a great idea 1%." That's why I asked the question (although I'd be willing to wager that 1% is a vast overstatement in this case :) – Matt Mills Aug 29 '12 at 15:47
  • Probably very much. I'd guess 0% (hence "probably not that likely" above). But let's say I'm wrong. If I was in charge of the decision, and I'd been totally sure of that 0%, then I've just messed up the person who hit on the 0.0001% exception. – Jon Hanna Aug 29 '12 at 15:52