0 Comments

When it comes to “String.Format”, we have always suffered the fact that it is so unreadable and error prone. When the string that you want to generate is long and has more than two or three variables, String class makes it very hard for us to have a readable text that you can find out what it is building.

Take a look at this string:

var myString = string.Format("The invitation is {0}. 
InvitationKey: {1}, UserKey: {2}, TenantKey: {3}",
Invitation.InvitationStatusKey.ToString(), Invitation.InvitationKey, Invitation.UserKey, Invitation.TenantKey);

 

Yes, I know, it is very hard to track down which variable is going to be put where! and will it even make sense!

But now looks like C# “String Interpolation” is going to clean this up for us. take a look at the simplified version of the above code in C# 6.0:

var myString = $"The invitation is {Invitation.InvitationKey}. 
                 InvitationKey: {Invitation.UserKey}, UserKey: {Invitation.TenantKey}, 
				 TenantKey: {Invitation.InvitationStatusKey.ToString()}";

 

And right after you make that change, you realize that you have been logging wrong information in to the place holders in your String.Format statement before, because you could not see what you are putting in the {0} placeholder! and now you can actually SEE what is it you are building!

I think this is one of the best features of C# 6.0 and for sure I’m going to spend some time removing all String.Format statements from out code as soon as we upgrade to it.

0 Comments

There is a very nice feature in C# 6.0 which will help us to remove a lot of “Magic Strings“ from our code.

I’m sure you use a lot of magic strings and it is completely normal to have them in cases like throwing an ArgumentNullException to mention the argument name. so the issue with that is when your argument is changed in the method, and you forget to update the magic string that represents the changed argument, your error will show an argument is null that does not exist in the code!

void MyFunction(string argument)
    {
    // Handle null argument.
    if (argument == null)
    {
        throw new ArgumentNullException("argument");
    }
}

And some other samples where raising OnPropertyChanged events where you have to call out what is the property that has changed and I bet you don’t like to do it with a magic string, because that property name could change and you might easily forget to update the magic string because the compiler will not complain about it. (You can find out about the miss match if you have unit tests for the body of your POCO classes which I doubt it!)

public string PersonName
      {
          get { return name; }
          set
          {
              name = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("PersonName");
          }
      }

Now, with introducing the “nameof” expression in C# 6.0, you can remove the magic stings and let the compiler take care of the refactoring that you do!

Here is how the above code snippet will look like using the nameof expression:

void MyFunction(string argument)
    {
    // Handle null argument.
    if (argument == null)
    {
        throw new ArgumentNullException(nameof(argument);
    }
}

The nameof expression only retrieves the final identifier, even if you use the fully qualified names. Also, in the case of attributes, the “Attribute” suffix isn’t implied. Instead, it’s required for compilation.

I think we can use this feature to to clean up some messy code.