March 28, 2024

C# Interview Questions

Here are some good, quick C# interview questions/answers gleaned from various sources:

What’s the implicit name of the parameter that gets passed into a property’s set method?

Value, and it’s datatype depends on whatever variable we’re changing.

How do  you handle multiple inheritance in C#?

MI is not supported.  You can use interfaces to simulate multiple inheritance, though it involves more code.

When you inherit a protected class-level variable, who is it available to?

The class in which it is declared and classes derived from this class.

Describe the accessibility modifier protected internal.

It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

Given that C# provides a default constructor for me, how many constructors do I need to write if I need one takes a string as a parameter?

Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

What’s the top .NET class that everything is derived from?

System.Object

How is method overriding different from overloading?

When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.

What does the keyword virtual mean in the method definition?

The method can be over-ridden.

How can you allow a class to be inherited from but prevent its methods from being overridden?

Make class public and make the method sealed.

What’s an abstract class?

A class that cannot be instantiated. Will have at least one abstract or virtual method that must be implemented/overridden.

Can you inherit/implement multiple interfaces?

Yes, why not.

What’s the difference between an interface and abstract class?

In the interface all methods are by definition abstract, whereas in the abstract class some methods can be concrete. In addition, interfaces do not define method accessibility, but this is required in abstract classes.

How can you overload a method?

Different parameter data types and/or different number of parameters. Changing the return value type is insufficient.

What’s the difference between System.String and System.StringBuilder classes?

System.String is immutable, System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

Describe the difference between a Thread and a Process?

A process is an OS-level application instance whereas a thread is a line of execution within a process.  A process can control multiple threads.

What is the difference between an EXE and a DLL?

An EXE is a fully-executable application and starts a new process when loaded, whereas a DLL functions as part of an application (and can be used by multiple applications).

What is a Windows Service and how does its lifecycle differ from a "standard" EXE?

A service is normally started by the OS at boot-time rather than by the user. Services do not usually present a user interface and are always running “in the background”.

What is strong-typing versus weak-typing? Which is preferred? Why?

Strong typing is preferred because the domain of values is constrained by the data type rather than by convention, as is the case when weak typing using System.Object and its related objects.

What is Reflection?

The ability for .NET code to examine and invoke other .NET code in executing or external assemblies. Commonly used to allow run-time decisions to be made about object instantiation.

Conceptually, what is the difference between early-binding and late-binding?

Early binding is done at compile time, whereas late binding happens at run-time.

Can DateTime, int, decimal, etc. be null?

No; however, any value type can be wrapped by System.Nullable to provide this functionality, as is commonly done in Linq2Sql or Entity Framework.

How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?

The garbage collector frees memory from the .NET run-time’s heap when it “gets around to it”.  It is non-deterministic in that objects can still exist even after they have gone out of scope and out of use.

How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?

A using() block guarantees that an IDisposable’s Dispose method will be called. IDisposable is an interface that’s implemented when a class uses resources such as files or COM objects that must be explicitly freed. Because you can count on Dispose to be called, the finalization of the uses of these resources is deterministic.

What’s wrong with a line like this? DateTime.Parse(myString);

The .Parse() method can raise an exception.  .TryParse() is a better way to do a conversion.

What is cyclomatic complexity and why is it important?

This term refers to the complexity of the loops and conditions within a class’ methods.  Methods with a higher level of complexity are often prone to bugs, difficult to maintain, and difficult to test.

Write a standard lock() plus “double check” to create a critical section around a variable access.

Per MSDN:

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}

Why is catch(Exception) {} almost always a bad idea?

Because swallowing the exception effectively amounts to ignoring all potential problems in the block. This might be appropriate for a specific exception type, but is not often desirable.

Contrast the use of an abstract base class against an interface?

These constructs can perform similar tricks for a developer and are somewhat interchangeable.  An abstract base class can contain method implementations whereas an interface cannot.  Conversely, interfaces work well with IoC containers for automatic instantiation of objects/dependency injection.

Is string a value type or a reference type?

Reference.

Why are out parameters a bad idea in .NET? Or are they?

There is nothing inherently wrong with out parameters.  They are used to return multiple values from a method, but a simpler way to achieve the same result would be to return an object with properties instead. 

What is a Postback?

A term originating with ASP.NET web forms, it refers to a page being posted back to itself for processing of data entered on the page.  Postbacks apply to MVC applications as well; generally our practice is to have a get action and a post action of the same name to handle form submission.

What is the one fundamental difference between XML Elements and Attributes?

One important, practical difference is that elements can repeat within the same level of a document, whereas only one attribute of a given name can describe a node.  Logically, an element is more of a noun-thing, whereas an attribute is more of an adjective-descriptor.

What is the following XPATH statement doing? myXmlDocument.SelectNodes("//mynode[name = ‘Joe’]");

Selects all nodes in the XML document having a subordinate name element with a value of Joe.

Leave a Reply