How Many Passes?

Large bodies of code written in the C/C++ languages typically divide up the code into “header” files, which are just declarations of methods and types (and definitions of macros). The actual bodies of those methods and types are in completely different files. People sometimes ask me why doesn’t C# need header files? which is a…

50

Style follows semantics

Which is better style? bool abc;if (Foo())  abc = Bar();else  abc = false; vs bool abc = Foo() && Bar(); ? To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” The stylistic choices should typically be driven by a desire to clearly…

44

Calling constructors in arbitrary places

C# lets you call another constructor from a given constructor, but only before the body of the calling constructor runs: public C(int x) : this(x, null){  // …}public C(int x, string y){  // …} Why can you call another constructor at the beginning of a constructor block, but not at the end of the block,…

28

Why are unused using directives not a warning?

As I’ve discussed before, we try to reserve warnings for only those situations where we can say with almost certainty that the code is broken, misleading or useless. One reason for trying to ensure that warnings are not “false positives” is that we don’t ever want to encourage someone to take working, correct code and…

56

What’s the difference between a destructor and a finalizer?

Today, another dialogue, and another episode of my ongoing series “what’s the difference?” What’s the difference, if any, between a “destructor” and a “finalizer”? Both are mechanisms for cleaning up a resource when it is no longer in use. When I was asked this, at first I didn’t think there was a difference. But some…

20

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

Why Can’t I Access A Protected Member From A Derived Class? Part Six

Reader Jesse McGrew asks an excellent follow-up question to my 2005 post about why you cannot access a protected member from a derived class. (You probably want to re-read that post in order to make sense of this one.) I want to be clear in my terminology, so I’m going to define some terms. Suppose…

22

Continuing to an outer loop

When you have a nested loop, sometimes you want to “continue” the outer loop, not the inner loop. For example, here we have a sequence of criteria and a sequence of items, and we wish to determine if there is any item which matches every criterion: match = null;foreach(var item in items){  foreach(var criterion in…

39

Is there such a thing as too much precision?

Well, enough chit-chat, back to programming language design. Suppose you’re building electronic piano software. As we’ve discussed before, the “equal temperament” tuning for a piano goes like this: the 49th note from the left on a standard 88 key piano is A, and its frequency is 440 Hz. Each octave above or below that doubles…

23

First Cousins Once Removed

Happy New Year all, and welcome to 2010, or, as my friend Professor Orbifold prefers it, MMX. I hope your festive holiday season was as festive and enjoyable as mine. The extended Lippert family continues to grow; this year at the annual Boxing Day party we needed two overflow tables for dinner instead of the…

28