The ?? Operator and Its Evil Uses

While I was reading some C# code recentrly I noticed a very interesting use of the ?? operator. Here are some tips on how to make best use of it:

First lets clear out a standard use of the operator. If the expression on the left of the oeprator is not null, it will be used, otherwise the expression on the right will be evaluated and used. For example we expose a property that will override a string that we will use.

Then, we will try to get the string from a resource file and lastly we will use a default value. Here is how we can do that without the ?? operator to turn this code:

//User defined property.
string messageToPrint = UserMessage;

//If not fallback to Strings resource.
if (messageToPrint == null)
{
    messageToPrint = Strings.ResourceMessage;
}

//Then fallback to a default message
if (messageToPrint == null)
{
    messageToPrint = "Default Message";
}

Console.WriteLine(messageToPrint);

into something shorter:

//This can be shortened to:
messageToPrint = UserMessage ?? Strings.ResourceMessage ?? "Default Message";
Console.WriteLine(messageToPrint);

//Or even this:
Console.WriteLine(UserMessage ?? Strings.ResourceMessage ?? "Default Message");

Also the null assignment operator can be used when assigning nullable value types to their non-nullable couterparts, also we can use the default keyword to help us if we do not mind what is the value we get:

Rect? nullableRect = null;

//Skip the nullableRect.HasValue check and always get a value:
Rect rectValue = nullableRect ?? default(Rect);

But then, here is the really evil use of the operator, i.e. an evil tip. This is the kind of thing you can use and if you understand it, it is ok but it will make the code unreadable for people that have no idea what this is. So it is good and bad at the same time :) .

The standard lazy initialization of a property:

private object myProperty;
public object MyPropeperty
{
    get
    {
        if (myProperty == null)
        {
            myProperty = this.CalculateMyProperty();
        }
        return myProperty;
    }
}

Then the evil one-liner:

private object myProperty;
public object MyPropeperty
{
    get
    {
        return myProperty ?? (myProperty = this.CalculateMyProperty());
    }
}

Be sparingly evil and enjoy :) .

Posted in Tips | Tagged , , | Leave a comment

Shortcuts of Shortcuts

Since windows Vista the task bar shortcuts can be accessed quite easily by pressing [winkey] + [number]

This also works on Windows 7 and is quite handy.

What I didn’t know is that you can assign keyboard shortcuts to shortcuts.

Just go to properties and press the shortcut you need with the ‘shortcut’ field selected. (it will be something like [Ctrl] + [Shift] + [Letter] or [Ctrl] + [Alt] + [Letter]

Shortcut in properties

Keyboard Shortcut of a Shortcut

Then you can run the shortcut any time it is visble and you press the buttons.

The interesting thing is that the IE favourites are also shortcuts and they seen to be always visible. So I assigned shortcuts to the often visited places and I can start them whereever I am, as long as the shortcut is not handled by the running program. Again, right click the shortcut and go to properties:

Properties of a Favourites Link.

Properties of a Favourites Link.

Hope this helps :)

Posted in Tips | Tagged | Leave a comment

Multiple Accounts in WordPress

While meddling with the settings of the wordpress blog, I had to supply the API key from the wordpress.com registration.

Initially I wondered why is this necessary, but it seems the account integrates with this instance of the blog quite nicely, it even goes into the main menu:

wordpress

Then stats andĀ other information also can be accessed fromĀ single dashboard!

I am happy :)

Posted in Uncategorized | Tagged | Leave a comment