Through the years, I have been in hundreds of interviews and phone screens as the interviewer or interviewee. Here are 10 of my favorite questions with answers. Not all of these are strictly specific to C# and might target the .NET Framework or core programming concepts.
-
What is C#?
C# is a 3rd generation general purpose programming language created by Microsoft. It is used across many different platforms including web (ASP.NET, ASP.NET MVC, ASP.NET Core), Windows Desktop (Windows Forms, WPF, UWP), Mobile (Xamarin, UWP), gaming (Unity3D, XNA), and web services (WCF, Web API) just to name a few.
-
What is an object?
This question requires a bit of caution because there are several different answers depending on the context. Typically, the context is object-oriented programming. In OOP, an object is a class. In computer science, an object is everything including a variable, function, pointer, etc. In databases, an object is a table, a view, a stored procedure, etc.
-
What are the four core tenets of Object-Oriented Programming (OOP)?
- Abstraction
Abstraction is a form of art, really. It is the act of hiding away the complexity of implementation. For example, the .NET Framework is a massive set of abstractions laid over the internal workings of the operating system you are targeting. You don’t need to understand how it interacts with the operating system. However, it is very helpful to understand how the .NET Framework classes work but not always necessary. If you look for them, you can find abstractions everywhere. - Encapsulation
Encapsulation has two fundamental parts. First, encapsulation is also known as information hiding. It enables you to protect or hide the implementation details or data because those details are not important to the implementor. Second, because the implementation details are hidden, it appears that an entire implementation chain is a single unit. A simple example of this is a public property that uses a private backing store in a class. The implementor cannot see anything past the public property but there might be dozens of operations hiding behind it. - Inheritance
Inheritance allows you to create a base class with functionality and reusing or overriding that functionality in classes that inherit or derive from that base class. - Polymorphism
Polymorphism means ‘many forms’. A real-world scenario may look like this. You are picking up data from a queue. You don’t really have any way of knowing what that data will actually but you do expect it to conform to some agreed-upon contract. If the data is type A, you will instantiate a new type A processor. If the data is type B, you will instantiate a new type B processor. And so on for type C, D, etc. To avoid creating processor-specific implementations, you just create an interface that all the processors implement and then you can call a specific method on each processor such as DoWork() and not really care which processor is actually running. This is also why polymorphism is also well known by the moniker ‘one interface, many functions’.
- Abstraction
-
Does C# support multiple inheritance?
No. There is no other acceptable answer. If you look at the definition of inheritance, it says class. It does not say interface. You do not inherit from an interface, you implement an interface. The answer is no.
-
Explain the benefit of implementing the IDisposable interface.
IDisposable allows for a very easy way for a class to release objects external to your application. This release will ensure that objects that implement the IDisposable interface are garbage collected and the external resources they use are no longer kept in use. For example, a connection to a database. Eventually, the database would run out of connections. Another example is an XmlWriter. Disposing the XmlWriter will release the StreamWriter object the XmlWriter references which will release the FileStream the StreamWriter references which will actually release the file handle used by the FileStream.
-
What are namespaces?
Namespaces are used to organize code into logical containers. For example, the System.Data namespace has classes that work with data and System.Configuration contains classes that work with configuration. We can apply namespaces to our own projects to gain as much control as we desire over the organization of our code.
-
Explain the different access modifiers available in C#?
There are several:
- public – Visible to all
- private – Visible only to the code in the same instance
- protected – Visible to the instance and any that inherit the instance
- internal – Visible only to the objects in the same assembly
- protected internal – Visible only to the instance and any that inherit the instance
- private protected – Visible only to the instance and any that inherit the instance in the same assembly
-
What are boxing and unboxing?
Boxing and unboxing is actually an important part of the .NET Framework’s common type system. Boxing is the process of converting a value type such as an integer to an object type. Unboxing is the process of converting the object type to a value type.
-
What is an interface?
An interface is a contract. It contains the signature or declaration of methods, properties, and events but never the actual implementation. When you implement an interface, you are agreeing to the contract of the interface and must explicitly implement all the containing methods, properties, and events.
-
What is the difference between a reference type and a value type?
I have written on this topic before. In short, a reference type is stored on the heap while a value type is stored on the stack. This is important because of the way you change the value. Read the linked article for a lot more information.
I hope you have enjoyed this post and possibly have learned a bit. I always enjoy reading interview posts and articles because they change over time. Recently, during a long span of really poor candidate quality, we returned to the basics as this post shows. These interview questions are very simple.