The coalesce operator, BKA the double question marks

13 January, 2009 § 4 Comments

In C#, there is a nice little operator that is rarely used. I’m not sure if this is because most people haven’t heard of it before or they just don’t know how to use it.

Basically, this operator allows you to check if an object is null, and if it is, return a different object.

Use this:

return userName ?? string.Empty;

Instead of this:

return (userName != null) ? userName : string.Empty;

Pretty simple, and it will save some typing. I’ve heard arguments against using the ternary operator from people claiming not to use it because fellow developers wouldn’t know what it is. That’s not the mindset we should have. If an operator exists in a language and there is a perfectly valid use for it, then go ahead and use it. When your colleage sees it, they will learn something about the language they never knew before and will most likely appreciate it.

Finally will execute, even after return

7 January, 2009 § Leave a comment

How’s this for weird looking code?

public static void RunSnippet()
    var y = testFinally();
    if( y == 99 )
        WL("It is 99");
        WL("It is 6");

public static int? testFinally()
    int? x = 99;
        return x;
        x = 6;   
    return x;

What happens? testFinally will return 99. If you changed the finally block to set x equal to null, the same result will happen. When there is a finally block, the function will not return until after the finally block has finished, regardless of any code following it. The second return statement is never reached. Also, you cannot return from a finally block, since control flow statements cannot be made in a finally block.

The code above was written with SnippetCompiler, a really nice free tool to have at your side when you want to test something out but don’t want to create a new solution in Visual Studio.

Extension Methods on Null Instances

6 January, 2009 § 1 Comment

I was reading some blog posts and came across one from Brad Wilson about a year ago. Extension methods are a pretty cool construct, allowing you to add instance methods to sealed classes easily. This brings with it a way to write code that can cause some weird readability issues. Often used is the static method String.IsNullOrEmpty. With an extension method you could add that method to instances and the code will work fine, even if the object is null.

Here’s a code sample:

using System;

public static class MyExtensions
   public static bool IsNullOrEmpty(this string str)
      return String.IsNullOrEmpty(str);

Now all Strings will have a method to check if they are null or empty. You can see that the readability would make you think that a NullReferenceException would be thrown if the object is null and the method wouldn’t get called. This is the odd part of extension methods.

The method will still get called, and the code will work like normal.

public static void Main()
   string str = null;
   bool strIsNull = str.IsNullOrEmpty();

INotifyPropertyChanged interface causing a XamlParseException?

22 December, 2008 § Leave a comment

I was working yesterday on a side project, and came across an unexpected exception when trying to debug the program. I hadn’t changed any of the XAML, but all of a sudden I was getting a XamlParseException with an ambiguous message when the program started up. It turns out that I was using the INotifyPropertyChanged pattern wrong.

If you have a property named ControlVisibility, then when you call PropertyChanged, you must make sure to create the new PropertyChangedEventArgs with “ControlVisibility” as the argument.

Shout out to Craig for helping me with this.

LDAP Basic auth over SSL using S.DS.P

5 December, 2008 § Leave a comment

I recently ran in to an issue where self-signed certificates were causing an LdapException to be thrown. The LdapException simply said “The LDAP server is unavailable.” Pretty ambiguous, huh? Well, long story short, the LDAP server had been configured with a self-signed SSL certificate, and that certificate wasn’t trusted on the machine I was using. How could I check this?

Well, there’s two ways to check this.

1.) I opened up the certificate manager and checked. Using Vista, click the Start menu and type “certmgr.msc” in the text field. Hit Enter and it will bring up the Certificate Manager for the machine. You can look for the certificate within a number of the folders shown.

2.) I added a callback method to the LdapConnection.SessionOptions variable. This turned out to be a necessary add for the future because I can now fully understand and report to a user why the connection was bad.

« Read the rest of this entry »

Where Am I?

You are currently browsing entries tagged with c-sharp at JAWS.