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.
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.
In the HTML and CSS snippet below we have two divs with a class called "inner". Both these divs are set to
display:inline-block; and hence they should display next to each other and not on top of each other. So why do they display on top of each other?
box-sizing is set to
border-box. Both inner divs are exactly 200px even with the 1px border and their parent is exactly 400px.
See this post on StackOverflow. Thanks to everyone who contributed.
It has been an incredible 9 weeks at FireBootCamp (21 Jul - 19th Sep 2014).
I went into FireBootCamp as a fairly experienced developer and I feel that I've come out so much better. My key technology highlights include AngularJS, Azure Websites, WebJobs, Storage Queues. I also learnt how to do Scrum properly in order to be Agile not Fragile.
FireBootCamp not only taught us about technology skills but also about the importance of helping others, the importance of communication skills and how to enhance your own career development.
One wise piece of career advice I received from my mentor Adam Stephensen who told me: "Don't get a job and only do the job". He advised me to specialise in something and become really good at it. Blog about, present it to user groups and to help people in the community when they need help. As a result I have made my first steps towards this and have started this blog to post solutions to problems or things which I have learned along the way and to help others who might stumble upon the same issues.
I have to thank all my fellow FireBootCampers for the incredible 9 weeks. Thank you to my mentors Adam Stephensen and Adam Cogan at SSW. Thanks to all the guest speakers. Thanks to all the SSW employees who have helped out when we were stuck on a difficult problem. Thanks to Marlon Marescia for your guidance and support. I am grateful.
This has got to be the best professional training course I've been to so far. As AdamS mentioned on our last day, the real work starts after the bootcamp. And so the work for me now begins…
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.