When would you use & on a bool?

UPDATE: A commenter points out that today is the 200th anniversary of the birth of George Boole; I had no idea when I scheduled this article that it would be so apropos. Happy birthday George Boole!


Here’s a little-known and seldom-used fact about C# operators: you can apply the & and | operators to bools, not just to integers. The & and | operators on bools differ from && and || in only one way: both operators always “eagerly” evaluate both operands. This is in marked contrast to the “lazily” computed evaluation of the && and || operators, which only evaluate their right hand argument if needed. Why on earth would you ever want to evaluate the right hand side if you didn’t need to? Why have this operation at all on bools?

A few reasons come to mind. First, sometimes you want to do two operations, and know whether both of them succeeded: Continue reading

Inferring from “is”, part two

In part one I gave a bunch of reasons to reject the proposed feature where the compiler infers additional type information about a local variable when inside the consequence of a conditional statement:

if (animal is Dog)
{
    animal.Bark(); 
    // instead of ((Dog)animal).Bark();
}

But the problem still remains that this is a fairly common pattern, and that it seems weird that the compiler cannot make the necessary inference.

A number of readers anticipated my denouement and made some of the same proposals I was going to make in this part. Let’s go through a few of them; see the comments to the previous article for a few more.

Continue reading

It’s still essential

I am excited to announce that Essential C# 6.0 is now available in stores!

As always, Mark did the vast majority of the work. And as always, I was delighted to be asked once again to contribute to one of my favourite C# books. The title is well-chosen; it really does give the essentials.

Mark and I are both cyclists (though he is in a lot better shape than I am!) and so I also love that every edition has gotten more and more bicycle on the cover:

2cover

3cover

4cover

5cover

6cover

Inferring from “is”, part one

In last week’s episode of FAIC I was discussing code of the form:

if (animal is Dog)
    ((Dog)animal).Bark();

Specifically, why the cast was illegal if the variable tested was of generic parameter type. Today I want to take a bit of a different tack and examine the question “why do we need to insert a cast at all?” After all, the compiler should know that within the consequence of the if, the variable animal is guaranteed to be of type Dog. (And is guaranteed to be non-null!) Shouldn’t the code simply be:

if (animal is Dog)
    animal.Bark();

Continue reading

Casts and type parameters do not mix

Here’s a question I’m asked occasionally:

void M<T>(T t) where T : Animal
{
  // This gives a compile-time error:
  if (t is Dog)
    ((Dog)t).Bark(); 
  // But this does not:
  if (t is Dog)
    (t as Dog).Bark();
}

What’s going on here? Why is the cast operator rejected? The reason illustrates yet again that the cast operator is super weird.
Continue reading

Nullable comparisons are weird

One of the C# oddities I noted in my recent article was that I find it odd that creating a numeric type with less-than, greater-than, and similar operators requires implementing a lot of redundant methods, methods whose values could be deduced by simply implementing a comparator. For an example of such, see my previous series of articles on implementing math from scratch.

A number of people commented that my scheme does not work in a world with nullable arithmetic (or, similarly, NaN semantics, which are similar enough that I’m not going to call out the subtle differences here.) That reminded me that I’d been intending for some time to point out that when it comes to comparison operators, nullable arithmetic is deeply weird. Check out this little program: Continue reading

Nature photography

I got a lot of great responses to my recent piece on features of C# I somewhat regret; thanks all for those.

As promised, today on fun-for-Friday-FAIC I’ve posted some fabulous adventures in nature photography from my recent trip to Lake Huron. Click on any image for a larger version. These were either taken by me or my friend Amber, who graciously loaned me her Canon DSLR, and taken with either the Canon or my GoPro.
Continue reading

Bottom ten list

Hey everyone, I am finally back from my many travels this summer and looking forward to doing some blogging this autumn. I’ll post some vacation photos when I have them sorted out. Until then, here’s an article that the nice people at InformIT asked me to write: what are my ten least favourite C# features?

I’m sure I missed some of your least favourites; I’d love to know what your pet peeves are in the comments here or on the article itself.