C# Code title image

C# Constructor Gotchas

A series of short posts focusing on some interesting aspects of constructors in C#.


In my previous employment, I was involved in the selection and interviewing of candidates applying for software developer positions. Although we already had a small set of standard technical questions which were put to each candidate during the interview, I was asked to come up with an additional set which could be given as a short written test.
I began compiling a few basic questions covering some important C# keywords such as new, using, yield, and lock. Although I had enough simple questions to use, I began to wonder whether the questions might only be useful in identifying those who had recently read through their C# textbook to prepare for such a test.
I then decided to try and write a few questions based around simple code snippets, asking the candidate to explain what the code would do or what would be output.
The questions I came up with were based on real examples of problematic code I had encountered in the past.

My hope was that the good candidates would take the opportunity to impress us by identifying why the code was bad and how it could be done better.
I also thought that giving the candidate an opportunity to discuss the questions and their answers (particularly the ones they got wrong) would offer insight into their ability to participate in technical discussions within the team.


I remain a little sceptical about the value added to our selection process by the test, and if I were in the same position again would probably do things a bit differently.

When trying to determine the suitability and effectiveness of some of the questions I persuaded some of my colleagues to answer the test in order to give a baseline against which we could compare candidates.
The number of incorrect answers and unanswered questions was somewhat worrying, but I hope that this also motivated them to learn and improve their knowledge in those areas.

More importantly, while writing some of the questions I discovered gaps in my own C# knowledge. This in turn gave me the opportunity to study and improve in those areas.

Perhaps the most (unexpected) benefit to me of having prepared technical questions for others was that I was well prepared for the kind of technical test questions I faced when I subsequently went elsewhere for interviews myself.


Regardless of the outcome or usefulness of the actual test, there are a few aspects from the code samples that I used back then which serve as useful examples of a particular behaviour in C#.

I have picked three aspects relating to constructors, and will discuss each of them here in a subsequent posts.

Leave a Reply