About Eric Lippert

Eric Lippert is a developer on the C# analysis team at Coverity. During his sixteen years at Microsoft he worked on Visual Basic, VBScript, JScript and C#, and was a member of the C# language design team. He can be found on Twitter at @ericlippert and blogs about C# at http://ericlippert.com.

String concatenation behind the scenes, part one

The first algorithm I ever worked on in the C# compiler was the optimizer that handles string concatenations.1 It's all pretty straightforward, but there are a few clever bits that I thought I might discuss today.

Continue reading

  1. Unfortunately I did not manage to port these optimizations to the Roslyn codebase before I left; hopefully someone will get to that!

What's the difference? sizeof and Marshal.SizeOf

I often see StackOverflow answers that confuse the sizeof operator with the Marshal.SizeOf method. These two operators do different things and can return different results, so it is important to know which is which.

In a nutshell, the difference is: the sizeof operator takes a type name and tells you how many bytes of managed memory need to be allocated for an instance of that struct.1 By contrast, Marshal.SizeOf takes either a type object or an instance of the type, and tells you how many bytes of unmanaged memory need to be allocated. These can be different for a variety of reasons. The name of the type gives you a clue: Marshal.SizeOf is intended to be used when marshaling a structure to unmanaged memory.

Another difference between the two is that the sizeof operator can only take the name of an unmanaged type; that is, a struct type whose fields are only integral types, Booleans, pointers and so on. (See the specification for an exact definition.) Marshal.SizeOf by contrast can take any class or struct type.

  1. I don't have to tell long-time readers of this blog that of course this is not necessarily stack memory; structs are allocated off the heap when they are array elements, fields of a class, and so on.

Construction destruction

Take a look at this little program skeleton:

class Foo 
{ 
    private int x;
    private int y;
    public Foo(int x, int y) 
    {
        this.x = x;
        this.y = y;
        SideEffects.Alpha(); // Notice: does not use "this"
    }
    ~Foo() 
    { 
        SideEffects.Charlie(); 
    }
}
static class SideEffects
{
    public static void Alpha() { ... }
    public static void Bravo() { ... }
    public static void Charlie() { ... }
    public static void M()
    {
        Foo foo = new Foo(1, 2); 
        Bravo();
    }
}

Let's suppose we have three side effects: Alpha, Bravo and Charlie. What precisely they are is not important.

The question is: what do we know about the order in which Alpha, Bravo and Charlie execute when a call to M() occurs?
Continue reading

What the meaning of is is

Today a follow-up to my 2010 article about the meaning of the is operator. Presented as a dialog, as is my wont!

I've noticed that the is operator is inconsistent in C#. Check this out:

string s = null; // Clearly null is a legal value of type string
bool b = s is string; // But b is false!

What's up with that?

Let's suppose you and I are neighbours.

Um... ok, I'm not sure where this is going, but sure.

Continue reading

What is lexical scoping?

Happy Eliza Doolittle day all; today seems like an appropriate day for careful elocution of technical jargon. So today, yet another question about "scope". As one of the more over-used jargon terms in programming languages, I get a lot of questions about it.

I'll remind you all again that in C# the term "scope" has a very carefully defined meaning: the scope of a named entity is the region of program text in which the unqualified name can be used to refer to the entity.1

Continue reading

  1. Scope is often confused with the closely related concepts of declaration space (the region of code in which no two things may be declared to have the same name), accessibility domain (the region of program text in which a member's accessibility modifier permits it to be looked up), and lifetime (the portion of the execution of the program during which the contents of a variable are not eligable for garbage collection.)

Quality assurance fail

PowerSupplySome fun for Friday. I just opened up a box containing a brand-new bit of telecommunications equipment, and the power supply arrived looking like this, fresh out of the box. (Click for a larger version.)

How bad does your quality assurance have to be to ship to customers a power supply that cannot possibly fit into a power socket?