Developer

C# developer interview questions and answers

Software development hiring managers and potential interviewees will find these open-ended C# proficiency interview questions and answers useful.

Good help is hard to find. There is an art to finding a developer who fits well in your organization in terms of personality and work ethic; fortunately, it's more straightforward to determine their technical expertise.

I worked at a couple of places where development managers loved drilling job candidates on syntax while having them write code, and it was clearly a stressful experience for the interviewee. I do not like asking specific syntax questions during interviews since most developers do not know language syntax or class names off the top of their heads.

In terms of the basic interview, I prefer to ask open-ended questions where the candidate can explain concepts and how they would attack problems. Some organizations like to give candidates tests or have them eyeball code snippets to spot problems, and I think those are good evaluation tools.

This is the first installment in our series of programming language-specific development interview questions and answers. Here is a list of questions (and the answers to those questions) that will help you get a feel for a candidate's proficiency with C#. You can ask follow-up questions based on their replies.

Note: This content is also available as a downloadable PDF.

What are namespaces, and how they are used?

Namespaces are used to organize classes within the .NET Framework. They dictate the logical structure of the code. They are analogous to Java packages, with the key difference being Java packages define the physical layout of source files (directory structure) while .NET namespaces do not. However, many developers follow this approach and organize their C# source files in directories that correlate with namespaces. The .NET Framework has namespaces defined for its many classes, such as System.Xml--these are utilized via the using statement. Namespaces are assigned to classes via the namespace keyword.

What is a constructor?

A constructor is a class member executed when an instance of the class is created. The constructor has the same name as the class, and it can be overloaded via different signatures. Constructors are used for initialization chores.

What is the GAC, and where is it located?

The GAC is the Global Assembly Cache. Shared assemblies reside in the GAC; this allows applications to share assemblies instead of having the assembly distributed with each application. Versioning allows multiple assembly versions to exist in the GAC--applications can specify version numbers in the config file. The gacutil command line tool is used to manage the GAC.

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.StringBuilder class should be used when string values will change.

What is DLL Hell, and how does .NET solve it?

DLL Hell describes the difficulty in managing DLLs on a system; this includes multiple copies of a DLL, different versions, and so forth. When a DLL (or assembly) is loaded in .NET, it is loaded by name, version, and certificate. The assembly contains all of this information via its metadata. The GAC provides the solution, as you can have multiple versions of a DLL side-by-side.

How are methods overloaded?

Methods are overloaded via different signatures (number of parameters and types). Thus, you can overload a method by having different data types, different number of parameters, or a different order of parameters.

How do you prevent a class from being inherited?

The sealed keyword prohibits a class from being inherited.

What is the execution entry point for a C# console application?

The Main method.

How do you initiate a string without escaping each backslash?

You put an @ sign in front of the double-quoted string.

String ex = @"This has a carriage return\r\n"

What is the difference between a struct and a class?

Structs cannot be inherited. Structs are passed by value and not by reference. Structs are stored on the stack not the heap. The result is better performance with Structs.

What is a singleton?

A singleton is a design pattern used when only one instance of an object is created and shared; that is, it only allows one instance of itself to be created. Any attempt to create another instance simply returns a reference to the first one. Singleton classes are created by defining all class constructors as private. In addition, a private static member is created as the same type of the class, along with a public static member that returns an instance of the class. Here is a basic example:

public class SingletonExample {
 private static SingletonExample _Instance;
 private SingletonExample () { }
 public static SingletonExample GetInstance() {
  if (_Instance == null)  {
    _Instance = new SingletonExample ();
   }
   return _Instance;
  }
}

What is boxing?

Boxing is the process of explicitly converting a value type into a corresponding reference type. Basically, this involves creating a new object on the heap and placing the value there. Reversing the process is just as easy with unboxing, which converts the value in an object reference on the heap into a corresponding value type on the stack. The unboxing process begins by verifying that the recipient value type is equivalent to the boxed type. If the operation is permitted, the value is copied to the stack.

Keep your engineering skills up to date by signing up for TechRepublic's free Software Engineer newsletter, delivered each Tuesday.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

53 comments
untitled98765
untitled98765

The problem I see with such a list of interview questions & answers is that if you are interviewed by a good interviewer and he asks one of the same questions which you have read through above, he can easily find out with one cross question, if your knowledge is only superficial. For each statement you read if you can't answer "why?" and "how?" you may have a tough time. As an example for the question:

Why are strings in C# immutable? I have attempted to post what should an interview reading material contain here: http://volatileread.com/Thread/Index?id=1038
PeggyJHawley
PeggyJHawley

Are there any more follow-up questions coming?

S164475
S164475

guys i am beginner in c# and i want to create Report Engine  that enables the user to process a data file and produce a report could include command line options, formatting options, sorting, help, output to screen or file

thank you and waiting for the best answer plz 


jhalaka
jhalaka

I really like this series! Will distribute to my staff. Keep it up!

gmaran23
gmaran23

I am glad I could answer all the questions posted. However, for the benefit of the tech community, please keep posting more and more questions on C# and .Net. More advanced topics on your hiring experience. Testing the syntax and API method overloads suits well for tech quiz and college grads. And not to disagree, those are top tier candidates IMO. I prefer software architechture/design questions and a generic pseudocode.

MadestroITSolutions
MadestroITSolutions

My personal approach has been a mix of both camps plus some personality analysis. The technical questions are good in order to evaluate a candidate's knowledge base. I do personally believe it's important to know what's happening under the hood so I usually throw some questions on that area. The sad reality is that very few candidates answer correctly and most of the ones that do have obviously memorized the stuff. I then throw some practical questions in the mix. This allows me to analyze the candidate's thought process and his/her ability to solve problems and think outside the box. The other thing I look at is personality. This is one of the toughest things to figure out since people looking for a job are usually very pleasing, lol. In the end what matters to me in this area is that they are team players with good communication skills.

Mark Miller
Mark Miller

To go along with this list, quizzing candidates for their technical skills, I remember years ago getting a preliminary technical quiz for a C++ position where I was given a class that had a bunch of problems. I was asked to find as many problems as I could. Some were syntax errors. Others were problems that would occur if the constructor, or certain methods were called, etc. It was a challenge, but it tested debugging skills as well as knowledge of how the language works.

sadiq zaheer
sadiq zaheer

Please tell me Any Book that is best for Database System..

AlphaCentauri
AlphaCentauri

I pose problems for candidates and then ask them to show me how they would go about solving the problem. Not knowing the answer is also valid, as is asking for help. Some problems are programming in nature, some are theoretical. The idea is to see if they can think critically, will fit in, be productive and use resources available to them. The questions in the article show someone who can memorize answers, which isn't what I am looking for.

Mick_S
Mick_S

I don't deal with these kind of technical questions anymore. I have been developing full time now for 5 years and I have been building websites since the mid 90's. If I didn't know what I was doing, I wouldn't still be working. If you are looking for an entry level programmer, then fine. Ask them some technical questions, but don't expect them to know everything, and don't hold it against them. After all, that's why they are entry level positions, and you are going to be asking your senior staff to train them. Any position higher than entry level and you are wasting your time, and if you are looking to fill a senior level position, asking someone to explain what a namespace is, or the difference between boxing and unboxing, is just plain insulting. On my resume I have listed my educational background, my work experience, and all the languages and applications I have proficiency in (and the level of that proficiency). A potential employer has everything they need to determine my knowledge and experience. If someone wants to ask about my approach to software design and engineering, I am happy to discuss it. If they start asking questions like the ones above that are designed to test my knowledge, then they are basically telling me they think I am lying on my resume, and that is not the way to start an employer/employee relationship. If I was heading an IT department and looking to staff it, I would make sure my senior staff have a certain level of experience, but also that they would be people who, if they didn't know the answer right away, they would know how to go and find the answer. On every project, application, or task I have worked on, I and my colleagues inevitably run into a programming challenge that we don't have the anwer to off the top of our heads. That's what the internet/MSDN/Google is for. Software development is about problem solving. Similarly, developing a software application boils down to problem solving. We're not taking a quiz. We're not trying to see how many technical details we can cram into our heads to impress people. The goal is to use every resource available to design and create the best application we can to solve our client's problem.

Slayer_
Slayer_

DLL hell was caused by new versions of DLL's breaking binary compatibility, how does .net fix that? Come to think of it, I just had a case of DLL hell with .net. Another application had registered an older version of a DLL our application uses, so it broke ours, and it won't let us re-register the newer version, the registry is messed up now.

tech
tech

Still waiting for the answer to this one....

sbone
sbone

While factual questions with definitive answers are good, I prefer to ask questions that require a deductive leap of some kind. Instead of asking what sealed is, give a scenario that requires the use of a sealed class, like a init on first use singleton in a multi-threaded environment. If they don't mark it as sealed in their implementation, then YOU should mark it as sealed and ask them why you did so. I see plenty of candidates that know the dictionary definition of everything, but do not know how to apply it in solving a real world problem. Another example would involve a struct with a few public members and Print() method for which you ask the interviewee for what it would output given a boxing situation. Then change 'struct' to 'class' and reask the question. This is MUCH better then asking what boxing and unboxing is, they need to identify it as they see it instantly. BTW, your assertions are not entirely accurate in the struct vs. class section (see 11.3 of the C# spec and Eric Lippert conversations on the subject), and I most assuredly would not even broach performance as relevant to this question, unless you are asking questions related to generic collections vs. Array for value types. In your answer for boxing and unboxing, you address type checking, but not null checking. Some people do not realize that you can throw a NullReferenceException in addition to the InvalidCastException. Some of your questions are very explicit and deep, others are so superficial and (frankly) condescending. I'd absolutely cringe if someone asked me what a constructor was, or how to escape a quote in a string. I'd have to evaluate the rest of the interview process, but asking questions like that make me thing poorly of the company and the interviewer. I like to stay away from syntax questions, as syntax is something that can easily be learned. I want my employees to be able to pick up any new language because they are excellent CONCEPTUAL OOP developers, so I spend my time going over philosophy and design. Getting bogged down in syntax/runtime minutia is not a useful thing to spend precious interview minutes with. Would I refuse to hire someone because they did not know your console app entry point question? Hell no. It wouldn't even factor into my consideration, thus I would not ask it. Why? 1) It is not something that would factor into the design of a system. 2) It is pretty simple to look it up. 3) You would never need it more than once in the entirety of the development of the system. As for the Singleton example, there are many implementations that have different effects. Your example is initialize on first use. Other examples could cause the singleton to be initialized at various times (I can think of two for sure, possibly three). I'm of the opinion that all singletons should be made thread-safe for maximum possible re-use (your example is not, but presenting it the way you have it allows for discussion and adjustment by the candidate to make it so). I'm not so sure that the 'DLL Hell' assertion is true given Win SxS (side-by-side) support in Windows XP and forward. New developers have possibly lived in a world where along with rotary phones, DLL hell never existed! This may have been a valid question at one point, but is well past its prime. Better to ask the pros and cons of using the GAC, and what requirements exist for putting an assembly in the GAC. Good luck with the rest of your series.

jayb
jayb

You stated that you don't like asking questions about syntax during an interview, but then you ask about the syntax for avoiding using backslashes. Since I always use blackslashes after years of C, C++, and Javascript programming, this isn't a C# feature that is of interest to me. I probably couldn't answer this question off the top of my head. You also ask what syntax to use to avoid inheritance - a better question might be "Discuss the different accesses available for classes" which should lead to a discussion of public, private, sealed, etc. and then to extension methods.

tooblessedtofail
tooblessedtofail

Wow! a refresher indeed. Infact there are some things I saw for the first time....can't wait for the second installment! Tony Pat- you did a great job here with the examples especially on Singleton design pattern. Tech Republic, you guys are AH-MAIZING! This came right on time. I am heading of for an interview RIGHT NOW...and I think it is gonna have some C# in the mix. :)

Tony Hopkinson
Tony Hopkinson

A basic GC question would be an idea and then you could put the singleton question in the questions for architectural cookie cutters.

gak1
gak1

Why ask such questions if it is possible to make anyone to know all the answers in a day? I guess it makes no sense to ask technical questions at this, evidently very entry, level. The immutable strings question is good, though. I guess the correct answer is "since the language must have the copy on write capability anyway, it have been easier to implement immutable strings on the language level and mutable strings on the library level than vice versa".

rpb_
rpb_

"What is the GAC, and where is it located?" You didn't answer the second part of this question.

Mark Miller
Mark Miller

"Why are strings in C# immutable?" Your answer doesn't really go with the question. It seems to me what you'd really want to ask (for the answer you gave) is, "How do you create a string that you can change?" This gets into the differences between immutable strings and StringBuilders. "What is DLL Hell, and how does .NET solve it?" The important distinction between native DLLs and .Net DLLs is that native DLLs do not have versioning information written into them, and when one with the same name and target path is copied onto the hard drive, it replaces the old DLL, hence the versioning conflict. My understanding is there's potentially this same problem with .Net DLLs, but .Net makes it possible within the GAC to have multiple versions of the same *shared* DLL, and versioning information is written into the DLL, so that the runtime can differentiate between them. "How are methods overloaded?" Your description is correct, but I thought it important to point out that overloading methods have the same names as the methods they're overloading, though their signatures (their parameter list, and return parameter) can be different. "What is the execution entry point for a C# console application?" The answer "the Main method" is basically correct, but there's too little information. It's not just a method called Main. Multiple classes could have a method called Main. It's a static method that is public as well. That information is important, since there can be only one of these.

Editor's Picks