Browse by Tags

Tagged Content List
  • Blog Post: 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...
  • Blog Post: 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);...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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 staff...
  • Blog Post: 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 stack when possible. The long answer...
  • Blog Post: The Stack Is An Implementation Detail, Part One

    " style="BORDER-TOP-WIDTH: 0px; DISPLAY: inline; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: 0px; MARGIN-RIGHT: 0px; BORDER-RIGHT-WIDTH: 0px" height=240 alt="Stack " src="http://blogs.msdn.com/blogfiles/ericlippert/WindowsLiveWriter/TheStackIsAnImplementationDetail_C978/Stack_thumb_1...
  • Blog Post: Mutating Readonly Structs

    Consider this program which attempts to mutate a readonly mutable struct. What happens? struct Mutable { private int x; public int Mutate() { this.x = this.x + 1; return this.x; } } class Test { public readonly Mutable m = new Mutable(); static void Main(string[] args) { Test t = new Test(); System...
Page 1 of 1 (11 items)