Software Development

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...

49 comments
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.

gak
gak

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.

Tony Hopkinson
Tony Hopkinson

That's part of the requirement for a self starter who can work alone isn't it. :( :(

Tony Hopkinson
Tony Hopkinson

Another I've had done to is a mini project. Oh and if you want a soft skills check the ffive minute presentation is a useful one. Avoid knob-head questions like Why are manhole covers round Name three of the overloads for the ToString() method and similar drivel.

aspatton
aspatton

This is a good point, I think a good interview will have different parts to get an overall feel for a candidate.

Tony Hopkinson
Tony Hopkinson

People have been know to 'erm exaggerate resume wise, or sometime a claim of experience is mere longevity, and sometimes it's misunderstood. I telephone interviewed one guy who claimed a decade of experience with a first class OO language and yet could not answer the equivalent of what's the difference between try catch and try finally. Said he'd never used either.... Saying that I agree with the spirit of what you are trying to say, anybody who equates answering these ten questions with being a competent developer deserves to employ imbeciles. It's a lot better than letting the buzzword bingo boys choose viable candidates though.

MadestroITSolutions
MadestroITSolutions

You can register multiple versions of a DLL with the GAC. Notice that is NOT the same as multiple files in the same folder. I don't know the internals of the GAC but when you register a DLL, I am sure it's storing it somewhere with some weird unique name. We don't care where it's stored or how it's stored, what matters is that the GAC is now tracking it. When your application runs, it will look in it's own folder first. If it can't find the DLL, it will look in the GAC. Your application can also be configured to request a particular version of a DLL. This is where the GAC works the magic. You could have multiple versions of a DLL registered and avoid breaking other applications that share it since they can now co-exist and use the appropriate version.

Tony Hopkinson
Tony Hopkinson

Pre .net. anyone could out a dll anywhere, "anyone" could twiddle with the registry key that located it. and many many anyones did, a lot. Out of the box .net looks in the GAC and to be in there it must have a strong name, so you have to explicitly load the wrong one, or in the same directory as the app. So if your problem child is a .net dll, someone has f'ed up big.

MadestroITSolutions
MadestroITSolutions

The way I understand it... Essentially, what is happening behind the scenes is that when you create a string, you are allocating a fixed amount of memory for it. If you assign a string of different length to the variable, what the runtime is doing in the background is requesting a memory space of the size of the new string, filling it up with the desired content and then releasing the old memory allocated to the prior string. This is why it's "immutable". A String Builder class on the other hand would allocate sufficient (or user-defined) memory space to conduct reasonable "mutations" (I love that word) of the string. In the event that the allocated space is no longer sufficient, it would reallocate internally.

Slayer_
Slayer_

That was the reason in Java anyways...

aspatton
aspatton

As far as syntax, I meant more along the lines of using certain classes/methods and such. I like your classes question.

Slayer_
Slayer_

Forgive me, I am self learning C#, so I don't know most of the terms and their differences.

sbone
sbone

How to make the singleton thread safe in C# is a better C# question. They don't need to know what one is (except for bonus points), but if you explain it, they should be able to code it. GC is subject to change based on your runtime (ASP.NET, the Compact Framework, Desktop, Silverlight, Mono) which makes a GC question more of a platform question. Personally, aside from making sure the candidate doesn't enjoy needlessly nulling variables and placing GC.Collect() and GC.WaitForPendingFinalizers() I'm quite happy.

aspatton
aspatton

I think asking such questions gives you a feel for a candidate and how they think or approach a problem. The questions can lead to further discussion which can shed more light on the person.

sbone
sbone

Since the GAC is addressed programmatically, and is an implementation detail of the runtime where it is does not matter. The fact that is does not matter where it is is the answer to the question.

aspatton
aspatton

Great points, you're hired!

Mick_S
Mick_S

Tony, I appreciate your position, but I still say these technical quizzes are not a good way to judge a competent employee. Glancing through the questions, I figure I could answer about 7 or 8 off the top of my head in a phone interview, but I probably wouldn't be using any of the buzz words to do it. I am self taught and switched careers late in life, but I had always been interested in computers and cut my teeth on Turbo Pascal in High School. When I first started interviewing for a full time developer position, I got all kinds of crazy questions, most of which I couldn't answer. One company even made me take a written test. It was all very demeaning, but I was going for entry level positions, and I needed someone to give me a chance, so I dealt with it. When I was finally offered a job, I was assigned as a subcontractor, given very little support, and my first assignment was to re-write some java servlets, which I had never done. Java, though, is pretty much syntactically the same as C# (which looks A LOT like Turbo Pascal - thank you Anders Hejlsberg). So on the job, I taught myself to use Net Beans, taught myself how java servlets interact, taught myself to use Apache, and got the job done. I know people pad their resumes, but all you have to do is ask someone to tell you about some of the projects they have worked on and the applications they have developed, and their skill and knowledge, or lack thereof, will reveal itself. My model employee will have technical skills and experience, but more importantly, they will be someone who has a passion for what they are doing, is not afraid to say they don't know something, and is willing to do what it takes to learn what they need to know to get the job done. Give me the guy who plods along but is dedicated to the job and will do what it takes to get his tasks done on time over the guy who passes the technical interview with flying colors but is out the door by 5:00 PM every day, and will leave you for another company in three years anyday.

Slayer_
Slayer_

Though that nearly defeats the purpose of a DLL. But this particular DLL is used by a variety of languages so it can provide common functionality to all of them.

MadestroITSolutions
MadestroITSolutions

Hi Slayer. The difference is that Singleton is a pattern while a static class is a class for which the runtime creates a single instance behind the scenes the first time you call it in code. This single instance is then used by any call to any static property or method of the class [type]. You could use a static class to implement the singleton pattern. There is an old debate about the implementation. Look it up on the Internet when you get a chance.

Tony Hopkinson
Tony Hopkinson

There can only be one of them, not much. However the Singleton pattern can be implemented in many languages, whereas how static a class is and what means is framework dependent, and it's the subtle differences that give you a right good kicking and several times... A Singleton can be pass as an argument, where as a static class can only be passed as a type. What and how the singleton works is up to you, static class its up to the language designers. Classes that implement singleton can implement interfaces. Singleton's can take advantage of generics Google Singleton, with it you can make any class a singleton, but you can instance a T for testing, If T implements an interface you can mock it for testing as well. Testing a static class with static fields, which it must have to be an alternative to a Singleton can be a right PIA.

Tony Hopkinson
Tony Hopkinson

As far as I can make out, knowing that much would but them in the top 20%. Lost count of the number of times I've heard, "I called GC.Collect and my code is still slow or I'm still getting an OOMs I got ten times as much out of reading the .net framework books than I did the C# one.

Tony Hopkinson
Tony Hopkinson

Or knew someone I could trust who knew them, I don't trust them. Make sure you get some team members in on the interview. A few open questions, about processes likes and dislikes for tools, interacting with "customers". More likely to get a definitely not than a definitely yes, but it all helps. Tick list questions should be closed, and they should be done before serious amounts of resource are put into interviews, where 99% of the questions should be open. I've got jobs before where I didn't actually know the language they wanted me to develop in, I knew a heck of a lot about the domain though and about how to develop. Attitude, then skill then familiarity with some tool or other. That's where the buzzword bingo boys keep going wrong they assume the first two and are incapable of understanding the relevance of the last.

aspatton
aspatton

I appreciate the feedback. I wouldn't think a quiz would be the single measuring stick for a candidate - just one part of the overall interview. Also, a lot of these quesitons (or create your own) are wide open, so they can foster a good discussion thus providing a peek at how a candidate thinks. As far as work ethic, that is a hard one to judge, checking references are a good indication.

Tony Hopkinson
Tony Hopkinson

indicates a good tech. A good employee is something else again. Thing is in the main we are being vetted by those who aren't techs, even the ones who were techs are usually well behind those of us who do it for a living, so they adopt a tick list style approach. When I give tests, I don't use them as a go / no go, more of trying t figure out where the candidate is, breadth and depth wise. I can tell whether someone is a programmer in a ten minute chat, with general questions like when would use use inheritance or aggregation. If they know that they'll be able figure out the syntax in c# with one google.

Slayer_
Slayer_

I just now you regasm /codebase /tlb the DLL, then add the TLB as a reference in the other languages (in our case, VBA)

Tony Hopkinson
Tony Hopkinson

They use the registry. Or someone could have implemented a non-standard resolution mechanism