C# Puzzles – Puzzle #1

Photo by David Clode

I think it is fun to find abnormal behavior in the language you use, but C# is a language that doesn’t have many abnormal behaviors. I plan on posting a few that I know, and hope to find more. This is my first puzzle and it stems back 10 years for me. I once spent some time reviewing every keyword in C#, and this was something I came up with from that.

Note: Scroll selectively to not spoil the results.

static void Main(string[] args)
{
    try
    {
        var lorem = "lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc rutrum porttitor nibh a imperdiet. Cras blandi ";

        var first = lorem[0];

        Console.WriteLine($"First Character: {first}");

        lorem = lorem.Remove(first);

        Console.WriteLine(lorem);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.GetType().Name);
    }

    Console.ReadLine();

}

Pretty basic function, getting the first character from the lorem string. Then we are removing the first character from the string.

What do you expect to happen?








Space to avoid spoilers.







It outputs ‘l’ for the first letter, but also outputs the lorem string without removing the ‘l’.

Umm OK.

Now lets change “lorem” to “zorem”.

static void Main(string[] args)
{
    try
    {
        var lorem = "zorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc rutrum porttitor nibh a imperdiet. Cras blandi ";

        var first = lorem[0];

        Console.WriteLine($"First Character: {first}");

        lorem = lorem.Remove(first);

        Console.WriteLine(lorem);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.GetType().Name);
    }

    Console.ReadLine();

}

What do you expect to happen now?








Space to avoid Spoilers.






It outputs the letter ‘z’, but now it throws an ArugmentOutOfRangeException. Any idea on why? Take a second to read over the code and see if you can figure it out.




Space to avoid Spoilers.


Well this is actually pretty easy to understand. The “first” variable is of course a character, but the “Remove” function isn’t what it seems. It is actually String.Remove(int startIndex). There is a implicit conversion between the char and int native types that automatically converts to a char to an int.

int ascii = 'a';

This above line compiles and works. What happens is your actually calling lorem.Remove(108) for ‘l’ and lorem.Remove(122) for ‘z’. The string is actually 108 characters long, so the first call would remove the trailing space. The second call with ‘z’ throws an ArgumentOutOfRange because the string isn’t 122 characters long.

Interestingly, although you can implicitly convert a char to an int, you cannot do it the other way around. Converting a int to a char is explicit like so:

char character = (char)122;

Hope you enjoyed this little puzzle. I’ll post more soon.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: