5 minutes read
The Ultimate Guide to C# Interview Questions

The Ultimate Guide to C# Interview Questions

C# has come a long way since its launch in 2000. It has been voted the 5th most popular programming language by TIOBE in January 2020. Originally developed by Anders Hejlsberg and his team as part of Microsoft's .NET initiative, over time it has become a multi-paradigm programming language that is not only widely used on the Windows platform but also the iOS/Android platforms. There is a heavy demand for developers who can code in C# and career opportunities have multiplied for those who are proficient. As a result, there has been a rapid growth in the number of online learning courses in C#.

Sololearn is one such remote learning platform that offers its users the opportunity to learn this language through its C# course tutorial. If you’re brushing up your C# skills for an interview, here are the 10 most probable questions with answers that will build your confidence and help you ace it.

10 Frequently Asked C# Interview Questions

Q1. What are the basic building blocks of object-oriented (OO) programming?

The two basic building blocks  of object-oriented (OO) programming are dynamic dispatch and the this pointer.  It is due to dynamic dispatch that polymorphic operations are possible in C#. Along with the this pointer, they serve as the tools required to make C# an object-oriented language.

While you may be tempted to include concepts such as polymorphism, encapsulation, inheritance, and abstraction in your response, remember those are the consequences of OO and not its building blocks. 

Q2. What does the this keyword do?

The this pointer is a pointer variable that refers to a current instance of a class. There are 3 main uses of this keyword in C#.

  • To qualify members hidden by similar names
  • To declare indexers
  • To pass an object as a parameter to other method

Q3. What is the meaning of dynamic dispatch?

In C# when you declare a member function as virtual (explicitly in a base class, or implicitly in an interface), you are telling the compiler you are using dynamic dispatch. 

The compiler emits code when these methods are called so that they are dispatched at run-time to the override implemented by the actual type of the instance.

Q4. Is it possible to execute multiple catch blocks?

Catch blocks are used to handle exceptions, and you can add a catch block for any/every exception in the program. However, even though there may be multiple catch blocks, only one of them will ever be executed. 

The order in which the catch blocks appear is significant here. Only the first catch block that matches the exception type will be executed. If you try to define two catch blocks for the exact same type, the C# compiler will generate an error.

Q5. What is meant by serialization?

Serialization and deserialization are used when data related to any object has to be sent from the source application to the destination application. Serialization is a process used to convert an object into a byte stream so that it can be transferred and stored in memory, database, or a file. For serializing an object, you must use SerializableAttribute. Otherwise, it will result in a SerializationException during run time.

Serialization is used whenever there is a strict need to preserve the original form of the data.

Q6. List the difference between class and struct?
Class and struct are both user-defined data types. However, there are a few key differences between them.

alt goes here

Q7. What are the different states in which a thread can exist in C#?
This question requires you to understand the life cycle of a thread in C#. A thread can exist in any of the following states:

  • Unstarted: In this state, the thread has been created but has not yet been executed.
  • Running: When a thread is performing an execution, it is said to be running. 
  • StopRequested: The thread has been requested to stop.
  • Stopped: When the ThreadStart delegate of a running thread is terminated, it enters the stopped state.
  • AbortRequested: The Abort method has been invoked on the thread, but the thread has not yet received the pending ThreadAbortException that will attempt to terminate it.
  • Aborted: The thread has received the ThreadAbortException, but has not yet stopped..
  • Background: The thread is being executed as a background thread, instead of as a foreground thread.
  • SuspendRequested: The thread has been requested to suspend.
  • Suspended: When the thread issues an input/output request, it enters the blocked state.
  • WaitSleepJoin: The thread is blocked.

Q8. What are the differences between the public/private/protected/internal access keywords?

Access modifiers are keywords used to specify the declared accessibility of a member or a type. The differences between the terms are:

  • Public: If a variable or method is declared public, access is not restricted and it can be accessed from anywhere.
  • Private: If the variable or method is declared private, access is limited to the containing type.
  • Protected: If the variable or method is declared protected, access is limited to the containing class or types derived from the containing class.
  • Internal: If the variable or method is declared protected, access is limited to the current assembly.

Q9. How do you go from C# code to a running program?

The 4 steps of code compilation in C# are:

  • Compiling the source code into managed code using the C# compiler.
  • Combining the freshly created code into assemblies.
  • Loading the Common Language Runtime (CLR) for execution.
  • Executing the assemblies using the CLR.

Q10. What is a reflection in C#?

This is an essential concept in C#. Reflection is defined as the ability of a code to get the metadata information of an assembly during runtime. This is done using the namespace System.Reflection.  There are several classes in System.Reflection.  The most frequently used are:

  • Assembly
  • AssemblyName
  • ConstructorInfo
  • MethodInfo
  • EventInfo
  • PropertyInfo
  • MemberInfo

Demonstrating that you know the core concepts of C# alongside the principles of object oriented programming will get you noticed by recruiters and help you shine in a technical interview. Learn C# online from anywhere, anytime, for free at SoloLearn.