Paul Kent

.NET Developer (AngularJS, jQuery, C#, Dependency Injection, Unit Testing, Onion Architecture, Azure Cloud)

How many ways can you think of to reverse a string in C#?

It's a seemingly easy programming task but dig a little deeper and it turns out to be a very tricky exercise. Here are three ways a C# programmer might do this using extension methods.

Note: Null checking is omitted for the purpose of demonstration.

All of these work however the Array.Reverse method turns out to be the neatest of the three approaches and also performs well.

But what happens if you wanted to reverse the string such as Les Mise\u0301rables that has an accent on the é? If you run the above code you get the output selbaŕesiM seL with an accent on the 'r' instead of the 'e'.

A better approach to solve this problem uses the .NET API for grapheme cluster iteration which was described as one answer by R. Martinho Fernandes on this StackOverflow post. It takes into account reversing strings which contain surrogate pairs.

The moral of the story is that string manipulation is not a simple as it first might appear to be. In fact there are many gotchas with all the basic data types we take for granted. Here is an excellent video presentation titled Jon Skeet - Stack Overflow Dev Days London 2009 by Jon Skeet that describes how the specification and implementation of basic data types like numbers, strings and dates causes a lot of headaches for Software Developers and is a source of many bugs. Also see Jon's blog titled OMG Ponies!!! (Aka Humanity: Epic Fail) which reiterates what he talks about in his video.

Can a C# object that contains an int be cast to a double?

We all know in C# that if you have an int you can cast it into a double like this:

int int_a = 5;
double dbl_a = (double)int_a;

But what if the int was boxed in a System.Object and you tried to cast it to a double like in the code below. Would it run?

object obj_intCastedToObject = (object)int_a;
double dbl_objectCastedToDouble = (double)obj_intCastedToObject; // Is this valid?

The answer is it compiles, but at runtime you get a System.InvalidCastException. Check out the following code snippet on how you could cast this correctly.

What do the C# keywords virtual, override and new mean?

What does the virtual keyword in C# do? Let's look at a practical example below in .NET Fiddle:

Here we have two classes: Car and Ford. Ford inherits from Car. The Car class has two methods Move() and Stop(). The Move() method in the Car class is declared virtual. In the Move() method in the Ford class we've declared that method with the override keyword.

On line 7 we are creating an instance of Ford but using polymorphism we can store it as a Car object because Ford inherits from Car. On line 8 in our code, when we call the Move() method on the car, the Move() method on the Ford class is still being called as can be seen from the resulting output. You can't call the Car's Move() method outside of the Car or Ford class. Only the Ford's Move() method is called. This is what the virtual and override keywords in C# provide for us.

The Microsoft documentation on the virtual keyword describes the behaviour as of it follows:

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

Let's now have a look at the new keyword. The new keyword in C# has two usages. One is to instantiate a class, the other is to hide an inherited method, like what we are doing on line 34.

The Stop() method in the Car class is not declared as virtual which is in contrast to the Move() method. On line 9 when we call car.Stop() notice how the Stop() method on the base method is called and not the Ford's Stop() method.

I hope this gives a general idea of these three keywords (virtual, override and new) and how behave in the context of inheritance and polymorphism.