1.1 100 C# Interview Questions
1.1 100 C# Interview Questions
Table of Contents 0
Introduction 4
About the author 4
The C# Questions 5
What is a reference type? 5
What is the value type? 5
What is the difference between class and struct? 6
What is an interface? 6
What is the difference between an interface and an abstract class? 6
Where do we use async and await in C#? 6
What are generics? 7
Can we use this keyword within a static method? 7
What is a constructor? 7
How many constructors can a class have? 7
What is method overloading? 8
What is LINQ? 8
What is the difference between ref & out parameters? 8
What is the difference between “as” and “is” operators? 9
What is the difference between “throw” and “throw ex” in C#? 9
Write an example of a try-catch block? 9
What are the most commonly used types of exceptions? 9
What are custom exceptions? 10
Why do we use reflection? 10
What are extension methods? 10
What is a virtual method? 11
What are anonymous types? 11
What are nullable value types? 11
What is the var keyword? 11
What are nullable reference types? 11
1
What is boxing? 12
What is unboxing? 12
What is the difference between Continue and Break keyword? 12
Which class acts as a base class for all classes in .NET? 12
What are the methods that System.Object provides to all other classes? 12
Can multiple catch blocks be executed? 13
What are the differences between System.String and System.Text.StringBuilder classes? 13
What is the difference between Finalize() and Dispose() methods? 13
Is C# managed or unmanaged code? 13
What is a Console application? 13
What is a jagged array? 14
What is serialization? 14
What are sealed classes? 14
What is the namespace? 14
What is the purpose of an access specifier in C#? 14
What is the scope of a public member variable of a C# class? 14
What is the scope of a private member variable of a C# class? 15
What is the scope of a protected member variable of a C# class? 15
What is the default access modifier for a class? 15
What is the use of Null Coalescing Operator (??) in C#? 15
Can you create a method in C# that can accept a varying number of arguments? 15
Can you pass additional types of parameters after using params in the function definition?
16
What is an enumeration in C#? 16
What is the difference between constants and read-only? 16
Can a private virtual method be overridden? 16
Describe the accessibility modifier “protected internal". 16
What are the circular references? 16
What is an object pool in .NET? 17
What are delegates? 17
How do you inherit a class in C#? 17
What are indexers in C# .NET? 17
What are C# attributes and their significance? 17
What is a preprocessor directive in C#? 17
Is operator overloading supported in C#? 18
Are multiple inheritances supported in C#? 18
How many ways can you pass parameters to a method? 18
What are the different ways a method can be overloaded? 18
2
Architecture 19
What are the Object-Oriented Programming principles? 19
How is encapsulation implemented in C#? 19
What are the SOLID principles? 19
What is a Dependency Injection? 20
What are IoC Containers? 20
What is the difference between inheritance and composition? 20
What are Design Patterns? 21
Explain the Factory Method design pattern. 21
Explain Command design pattern. 21
Explain the Decorator design pattern. 21
Explain the Singleton design pattern. 22
Explain the Observer design pattern. 22
Explain the Adapter design pattern. 22
Explain the Abstract Factory design pattern. 22
Explain the Chain of Responsibility design pattern. 22
Explain the Builder design pattern. 22
Explain the Facade design pattern. 23
Explain the Template Method design pattern. 23
Explain the State design pattern. 23
Explain the Composite design pattern. 23
Explain the Memento design pattern. 24
Explain the Prototype design pattern. 24
Explain the Proxy design pattern. 24
Explain the Visitor design pattern. 24
Explain the Bridge design pattern. 24
Explain the Iterator design pattern. 24
Explain the Mediator design pattern. 25
Testing 26
What is unit testing? 26
What is the structure of a unit test? 26
What is code coverage? 26
What is integration testing? 27
What is UI testing? 27
What is a testing pyramid? 27
What are stubs? 27
What are mocks? 27
What are mocking libraries? 28
3
What is Test-Driven Development? 28
What is refactoring? 28
Conclusion 28
4
Introduction
Welcome, dear colleague developer. I’ll keep this introduction short since no one reads this
anyway.
The purpose of this eBook is to collect at one place all C# interview questions that might pop
up at your next interview.
I have a favor to ask: if you see I made a mistake anywhere in this ebook or would like to
suggest a question, please let me know at kristijan@blueinvader.com.
Ok, enough with this introduction! Let’s start with the questions.
5
The C# Questions
The following keywords are used to define reference types: class, interface, and
delegate. C# also has built-in reference types: dynamic, object and string.
then 3 in memory is stored directly in the number variable. When you pass the value type
variable to a method or assign it to another variable, then its value is copied. This means that
change to one variable does not affect the value of another variable.
Some examples of value types are bool, int, decimal, double, enum and struct.
6
3. What is the difference between class and struct?
Classes are reference types, while structs are value types. Classes support inheritance and can
be null, and should be used for complex objects. Structs don’t support inheritance, they are
value types and are typically used to represent small objects.
4. What is an interface?
An interface defines a contract. Any class or struct that implements that contract must provide
an implementation of the members defined in the interface. Beginning with C# 8.0, an
interface may define a default implementation for members. It may also define static
members in order to provide a single implementation for common functionality.
The other difference is that a class can inherit only one abstract class, while it can implement
multiple interfaces.
async keyword is used while declaring an asynchronous method, while await is used to call
that kind of method.
7
7. What are generics?
Generic class, struct, method, or interface is used to define a code that doesn’t have a
concrete type.
Example:
// Declare the generic class.
public class GenericClass<T>
{
public void SomeMethod(T input) { }
}
This enables us to write one functionality, but reuse it for multiple types. Instead of using a
specific data type, we use a generic type parameter, usually T.
9. What is a constructor?
A constructor is a special type of method that is used to create a new instance of an object.
The constructor has the same name as the type it creates. When you create a new class/struct,
by default you get a default constructor that can be used to create a new instance of that
class/struct.
8
11. What is method overloading?
Method overloading is having several methods with the same name, but they have different
signatures (different parameters). Multiple methods can have the same name as long as the
number and/or type of parameters are different.
// LINQ Query
var result = from name in developmentLanguages
where name.Contains('a')
select name;
9
14. What is the difference between “as” and “is” operators?
The as operator is used to cast an instance of an object to a particular type. If it fails, it will
return null.
The is operator is used to check whether an object can be cast to a particular type. It returns
true if the object can be cast to a particular type, false if it can’t.
● NullReferenceException
● StackOverflowException
● ArgumentOutOfRangeException
● OutOfMemoryException
● DivideByZeroException
● IndexOutOfRangeException
10
18. What are custom exceptions?
In some cases, we want to throw exceptions that are specific to our application domain. In
order to do that we can define a custom exception type. That type inherits from some
exception type.
The next example shows how to extend DateTime type to provide a new method to it, to
check whether or not the day of the week for a date is the weekend:
public static class DateTimeExtensions
{
public static bool IsWeekend(this DateTime dateTime)
{
return dateTime.DayOfWeek == DayOfWeek.Saturday ||
dateTime.DayOfWeek == DayOfWeek.Sunday;
}
}
11
21. What is a virtual method?
A virtual method is a method that can be overridden in the subclasses. A virtual method has
an implementation in the base class but the subclass can choose to replace that
implementation. We use the virtual keyword to indicate that the method can be
overridden.
● A variable of a reference type T must be initialized with non-null, and may never be
assigned a value that may be null.
12
● A variable of a reference type T? may be initialized with null or assigned null, but is
required to be checked against null before dereferencing.
● A variable m of type T? is considered to be non-null when you apply the null-forgiving
operator, as in m!.
Nullable reference types aren't new class types, but rather annotations on existing reference
types. The compiler uses those annotations to help you find potential null reference errors in
your code.
29. Which class acts as a base class for all classes in .NET?
The Object type is used as a base class for all other classes in .NET. An object is used as an
alias for System.Object class. To an instance of the object class may be assigned values of
any other type, whether it’s reference or value type.
● Equals
13
● Finalize
● GetHashCode
● ToString
31. Can multiple catch blocks be executed?
There is no way multiple catch blocks with a similar type can't be executed. Only one catch
block can be executed at the time.
14
36. What is a jagged array?
A jagged array is an array that consists of elements of a type array. A jagged array is also called
an array of arrays.
15
42. What is the scope of a private member variable of a C#
class?
The scope of a private member variable of a C# class is to hide its member variables and
methods from another method. Only methods of the same class are accessible by its private
members. Even an instance of a class is just an instance and can't be accessed by its private
members.
If the value of the first operand is empty, the operator returns the value of the second
operand; otherwise, it returns the value of the first operand.
16
47. Can you pass additional types of parameters after using
params in the function definition?
No. Additional parameters are not allowed after the keyword params. Only one keyword
params is allowed in the declaration of the method.
C# enumerations are a type of data value. In other words, the enumeration contains its very
own values and therefore cannot pass inheritance.
17
53. What is an object pool in .NET?
The object pool is a container that houses objects ready to be used. It records the total
number of items actually in use in the pool, thereby reducing the overhead for making and
re-creating objects.
The main advantage of a preprocessor like #ifdef is generally to make source programs easy to
change, compile, and run in different execution environments.
18
59. Is operator overloading supported in C#?
Most built-in operators usable in C# may be redefined or overloaded.
Overloaded operators are methods with a unique identity or specifier, the keyword operator,
preceded by the operator symbol being specified. Similar to every other method, the
overloaded operator has a fixed return form and a set of parameters.
19
Architecture
● Abstraction - Objects only provide access to methods and properties that are relevant
to the use of other objects, and hide implementation details.
● Encapsulation - The implementation and state of each object should not be exposed
to the public.
● Inheritance - Code can be reused through the hierarchy.
● Polymorphism - At run time, objects of a derived class may be treated as objects of a
base class in places such as method parameters and collections or arrays.
20
The principles are:
If a developer follows these principles, then it should be easy for him to create applications
that are easy to maintain and extend.
The advantage of dependency injection is that the object who wants to call some services
doesn’t have to know how to construct those services. Instead, the object delegates the
responsibility of providing its services to an external code.
21
Inheritance is a type of relationship between objects where they are arranged in a hierarchy.
This is an "is-a-type-of" relationship. For example, a class Worker might inherit from the
class Person. This means that the Worker class is a type of Person.
In general, preferring composition over inheritance is a design principle that gives your code
higher flexibility.
It is not a finished design that can be transformed directly into source or machine code.
Rather, it is a description or template for how to solve a problem that can be used in many
different situations. Design patterns are formalized best practices that the programmer can
use to solve common problems when designing an application or system.
22
73. Explain the Singleton design pattern.
Singleton is a design pattern that ensures that a class has only one instance while providing a
global access point to this instance.
23
special interface that hides almost all of your class's implementation details from the client.
The "builder" interface defines a sequence of methods that the client can call to manipulate
the object's state and get it ready for use. In this way, the client can customize an object's
behavior and state without having to know how the class is implemented.
24
83. Explain the Memento design pattern.
The Memento design pattern encapsulates the state of an object into a secondary object, the
memento, which stores all the information about the object in an easy-to-access way. This
way, you can save the object into persistent storage, and restore it into a new object when
needed. The pattern is used when you need to serialize an object and deserialize it later on.
25
Iterator pattern is a creational pattern in which an iterator interface is implemented by a class
that enumerates its elements. This iterator interface enables the elements to be accessed one
at a time in a sequential manner, through the iterator.
26
Testing
The purpose of unit testing is to confirm that each unit of the software performs as designed.
A unit is a small piece in a codebase, usually a single method in a class, but it can be the class
itself.
27
measured as a percentage, has had more of its source code executed during testing, which
suggests it has a lower chance of containing undetected software bugs compared to an
application with low test coverage.
With UI testing we can find and interact with UI elements, and validate UI properties and state.
However, the main difference between the mock and the stub is that the mock usually verifies
that the class under test behaves as expected. The mock checks that some method was called
28
and/or that it was called with correct parameters. In the assert part of the test, you check your
assumptions against the mock.
The popular mocking libraries in .NET are moq, NSubstitute and FakeItEasy.
29
Conclusion
Congratulations! You’ve made it to the end! You are now one step closer to getting that C# job
you want.
Best of luck!
Kristijan Kralj
30