Inheritance and Representation

(Note: Not to be confused with Representation and Identity) Here’s a question I got this morning: class Alpha<X>   where X : class {}class Bravo<T, U>   where T : class   where U : T {  Alpha<U> alpha;} This gives a compilation error stating that U cannot be used as a type argument for…

12

To box or not to box, that is the question

Suppose you have an immutable value type that is also disposable. Perhaps it represents some sort of handle. struct MyHandle : IDisposable{    public MyHandle(int handle) : this() { this.Handle = handle; }    public int Handle { get; private set; }    public void Dispose()    {        Somehow.Close(this.Handle);    }} You might think hey, you know, I’ll decrease my…

59

Debunking another myth about value types

Here’s another myth about value types that I sometimes hear: “Obviously, using the new operator on a reference type allocates memory on the heap. But a value type is called a value type because it stores its own value, not a reference to its value. Therefore, using the new operator on a value type allocates…

40

The Truth About Value Types

As you know if you’ve read this blog for a while, I’m disturbed by the myth that “value types go on the stack”. Unfortunately, there are plenty of examples in our own documentation and in many books that reinforce this myth, either subtly or overtly. I’m opposed to it because: It is usually stated incorrectly:…

68

A Definite Assignment Anomaly

UPDATE: I have discovered that this issue is considerably weirder than the initial bug report led me to believe. I’ve rewritten the examples in this article; the previous ones did not actually demonstrate the bug.  Consider the following code: struct S {  private string blah;  public S(string blah)  {      this.blah = blah;  }  public void Frob()  {…

22

Always write a spec, Part Two

Upon submitting that specification for review, even before seeing my code, Chris found a bug and an omission. The omission is that I neglected to say what happens when the ref variable is an index into a fixed-size array buffer. As it turns out, that case is also rewritten as a pointer dereference by the…

20

Not everything derives from object

I hear a lot of myths about C#. Usually the myths have some germ of truth to them, like “value types are always allocated on the stack“. If you replace “always” with “sometimes”, then the incorrect mythical statement becomes correct. One I hear quite frequently is “in C# every type derives from object”. Not true!…

47

Null Is Not Empty

Back when I started this blog in 2003, one of the first topics I posted on was the difference between Null, Empty and Nothing in VBScript. An excerpt: Suppose you have a database of sales reports, and you ask the database “what was the total of all sales in August?” but one of the sales…

33

The Stack Is An Implementation Detail, Part Two

A number of people have asked me, in the wake of my earlier posting about value types being on the stack, why it is that value types go on the stack but reference types do not. The short answer is “because they can”. And since the stack is cheap, we do put them on the…

23