Recently, on behalf of my employer, I spoke at the IT Academics Day, a small .NET conference organized by the West Pomeranian University of Technology. Together with Mateusz, we have presented some of the traps that a C# developer might encounter and how to recover from those. I would like to share three of ten such traps in today’s blog post, the rest will follow.
When inheritance is dangerous
The first one is a dangerous design trap, a bomb with a long fuse. Let’s take a look at the following code:
internal class BaseData { protected string data = null; public BaseData(string data) { this.data = data; this.InitializeState(); Console.WriteLine("BaseData created"); } public virtual void InitializeState() { // Some basic initialization logic. } }
Do you see anything dangerous yet?