I'm often surprised by the low quality of answers I get from the candidates during interviews. Technical questions quickly show a deep misunderstanding of the basic concepts, and I'm wondering how could it be that a person who claims being a professional C# programmer for five years is unable to explain what
IDisposable is or what
sealed keyword is used for.
I always thought that those candidates don't even care preparing themselves for interviews, but some ensured me they do. Then I took a look at the websites which appear in Google when searching for "c# interview questions". Indeed, the poor quality of most websites and the complete misunderstanding of C# and .NET Framework by the authors may be accountable for the time we waste doing interviews with candidates who won't be hired anyway.
Let's see a few examples.
What is the difference between public, static and void?
All these are access modifiers in C#. Public declared variables or methods are accessible anywhere in the application. [...]
The author has a complete misunderstanding of basic C# concepts. There are four access modifiers in C#: public, protected, internal, private. A public method isn't necessarily accessible anywhere in the application. A public method of an internal class cannot be called from a different namespace.
Void type indicates that the method doesn't return a value. In an
void can also be used to declare a pointer to an unknown type.
The static modifier, when used at the level of a member, indicates that this member is static, i.e. belongs to the type itself rather than to a specific object. When used at the level of a class, it indicates that all the members of the class are static.
Why are strings in C# immutable?
Immutable means string values cannot be changed once they have been created. Any modification to a string value results in a completely new string instance, thus an inefficient use of memory and extraneous garbage collection. The mutable
System.Text.StringBuilderclass should be used when string values will change.
So why are they immutable? It seems that the author doesn't know the answer, since he doesn't tell us the reason strings are immutable.
The actual reason is that immutable objects are:
- Easier to design and use, since they have one and one state only,
- Are thread-safe,
- Can be shared without having to clone them.
As usual, all those reasons are given by an excellent answer. on Stack Overflow. Instead of telling us exactly this, the author blames immutability for an inefficient use of memory.
What is a destructor?
A destructor deletes an object of the class from memory. It is called when the object is explicitly deleted by the code you write, or when the object passes out of scope, which can happen when the program exits a function. The destructor has the same name as the class, but with a tilde prefix.
Completely wrong. Destructors are syntaxic sugar for finalizers. The “when the object passes out of scope” indicates that the author has no understanding of C# whatsoever; otherwise, the author would have heard about Garbage Collector and that “The programmer has no control over when the destructor is called because this is determined by the garbage collector.”
Describe delegate in detail.
A delegate is an object that can refer to a method. It means that it can hold a reference to a method. The method can be called through this reference or we can say that a delegate can invoke the method to which it refers. The same delegate can be used to call different methods during the runtime of a program by simply changing the method to which the delegate refers. The main advantage of a delegate is that it invokes the method at run time rather than compile time. Delegate in C# is similar to a function pointer in C/C++.
A delegate is not an object: it is a type. A delegate instance is an object. The rest of the explanation is so unclear that I can't even tell if there is something right in it. In all cases, when the author asserts that “The main advantage of a delegate is that it invokes the method at run time rather than compile time”, it is clear that he has no understanding of neither delegates, nor the difference between runtime/compile time. There is no such a thing as a method invoked at compile time.
So what is a delegate?
It is a type that represents references to methods with specific parameters and a specific return type. A delegate instance is a reference to a method which has the same parameters and return type as the delegate.
As for the advantages, delegates have multiple benefits and there is no single "main advantage". For example, it allows to define a callback method.
Looks good. I'm happily surprised by the questions and answers: the fact that answers are short is fine too: we don't expect long, detailed answers during the Q&A part of an interview.