A colleague and I had a discussion the other day about the use of curly brackets in C# and when to use them. The use of curly brackets is of course a part of the C# syntax, but you can use them in various different ways. We discussed the two line if-statement, because that’s the only scenario we found us to be in doubt.

Here are two ways of writing the same if-statement with and without the use of curly brackets.

With curly brackets:

if (foo == "enabled")
{
   return true;
}

Without curly brackets:

if (foo == "enabled")
   return true;

We both come from the Visual Basic world where you don’t have curly brackets, and that’s probably why we had this discussion in the first place. The question is which approach is the best? I know it’s a stupid question and a very subjective one, but I’m interested in your opinion on this. What do you think?

Today, I had to do file renaming on 1.000 files in a directory so they could be sorted alphabetically. I will not go into details, but it was a lot trickier than anticipated because the original filenames where very different.

Basically, I wanted all filenames to be 4 digits long, so that 1.psd became 0001.psd and 754.psd became 0754.psd

In order to do that, I needed a method that would prefix any given number with the right number of zeros. So I came up with this:

private static string ForceLength(int number, int length)
{
   string s = number.ToString();
   string returnString = "";

   for (int i = s.Length; i < length; i++)
   {
      returnString += "0";
   }

   return returnString + s;
}

If you pass 54 as the number parameter and 4 as the length parameter, the method returns "0054".

I’m not sure this is the best way to go, but it works. What do you think?