C# Interview Questions PDF
C# Interview Questions PDF
1. What Is C#?
You can use C# to create apps for Microsoft Windows, cloud-based API services, mobile apps
for iOs and Android, software for AI and Machine Learning (ML), blockchain apps, and
serverless apps.
Note, this is the most fundamental C# interview question, so make sure you know it!
This is one of those simple C sharp interview questions that you should know in your sleep!
Classes have properties and methods, which are referred to as class members. These could be
private, which means that they are only accessible to the class, or public, which allows other
parts of a program to access them.
Objects are class instances that can represent the characteristics of real-world entities (or
imaginary!) by using class attributes and methods.
For example, if we have a program that handles books, we can create a Book class with two
properties, the title, and the author. We can then instantiate multiple instances of the Book class
to create different Book objects with various titles and authors.
While all classes in C# have a base type of Object, there are 4 distinct types.
● Static Class: Cannot instantiate objects with the New keyword, and class members can
only be accessed by using the actual class name
● Abstract class: Restricted, which means it cannot be used to create objects directly, so
we need to inherit from this via a sub-class if we want to access its members
● Partial Class: Allows methods, events, and properties to be split into separate/multiple
.cs source files, which are combined into one class at compile time
● Sealed Class: Prevents users from inheriting from the class, and restricts access to the
class members such as properties or methods
● Break: Used in loops (for, etc.) and switch statements, exits the iteration/switch and
skips any remaining code in the loop or switch block
● Continue: Only used in loops, skips any remaining code in the loop and starts the next
iteration from the beginning of the loop
● Try: Used to signal a block of code that handles errors, followed by at least one catch
● Catch: Used to handle exceptions raised by code within a try block
● Finally: Signals a block of code that almost always runs after all other code has
executed, regardless of whether an error is handled
● Throw: Used to throw an exception if a problem is encountered, and usually requires an
if statement to check for a problem
The two accessors in C# are get and set. These are used to retrieve or assign values for the
private fields related to a property within a class or struct.
This is one of the most fundamental C# programming interview questions, and it should be
automatic for any prospective developer.
The name static declared variables relates to an unchanging value (static keyword) that is
strongly typed (declared), but usually, these are just called static variables.
Value Type: Directly stores the variable data value inside its allocated memory space
Reference Type: Does not directly store the variable data in its memory location but instead
stores a reference to another memory address that holds the variable data value, so the
equivalent of a pointer
Namespaces are used to organize code and provide a way to separate code that may utilize the
same names.
The .NET framework uses namespaces to separate and organize its extensive collection of
provided classes, but you can also use custom namespaces to organize your own code and
limit the scope of method and class names within your projects.
Want to boost your interview prep? Refresh key concepts by checking out the 10 Best C#
Books
To manage and automatically release resources once an object has finished its processing.
Specifically, by implementing the using statement, we implicitly ensure that the .Dispose()
method is called to automatically release unused resources.
Both methods have a similar purpose, namely to release unused resources. However, unlike the
.Dispose() method, .Finalize() does not guarantee the garbage collection of any unused and
released resources.
● Boxing: Refers to an implicit process that is used to convert a variable from a Value
Type to a Reference Type data type
● Unboxing: Refers to the opposite of the boxing process, meaning that it represents an
explicit conversion process where we convert a Reference Type data type into a Value
Type variable
● Value parameter: The default option, parameters that are passed into a method will
result in a new copy being stored in memory, and any changes to this copy will not be
reflected on the original value parameter
● Reference parameter: Stores the memory address of a parameter within the method
argument, so any changes to the argument are reflected on the original parameter in
memory
● Output parameter: This is used when you want to a method to return multiple values
In order to assign a null value to Value Types in C#, we have to use the Nullable Type. This can
be done with either the Nullable keyword or the ‘?’ operator shorthand, as shown below.
It is not possible to use the Nullable Type with var type variables because these are not explicitly
defined but rather implicitly defined by the value assigned to the variable.
Note that this is one of those C# important questions you need to understand properly.
Nullable<int> i = null;
int? i = null;
Constructors are a special type of function member from a class that shares the same class
name. They are automatically invoked whenever a new object instance of the class is created,
and this process also invokes the data members of the class. This may involve passing
parameters into the class constructor if it is parameterized.
These C# class methods are used to destroy class instances when they’re no longer required.
Destructor methods are not called explicitly within a program, but rather the .NET garbage
collection calls these methods implicitly when an object is no longer accessible (not in scope).
To define a destructor, we use the class name and precede this with a tilde, as shown in the
example below.
class Example {
~Example() {
Console.WriteLine("Destructor has been called.");
}
}
Each non-generic collection can be used to store different types as they are not strongly typed.
● ArrayList: Similar to an array, does not have a specific size, and can store any number
of elements
● HashTable: Stores key-value pairs for each item, does not have a specific size, can
store any number of elements, key objects must be immutable
● SortedList: Combination of ArrayList and HashTable, data stored as key-value pairs,
items sorted by keys, items accessed by key or index, does not have a specific size, can
store any number of elements
● Stack: Simple Last-In-First-Out (LIFO) structure, does not have a specific size, can store
any number of elements, elements are pushed onto the stack and popped off from the
stack
● Queue: First-In-First-Out (FIFO) structure, does not have a specific size, can store any
number of elements, elements are enqueued into the queue and dequeued from the
queue
A jagged array is made up of elements that are also arrays, but each of these member arrays
can be a different size. Because of this composition, we can refer to a jagged array as an ‘array
of arrays’.
File handling can refer to the process of opening, reading, writing, and appending to and from
files. The two most common file-handling operations are reading and writing, and in each case,
the file’s data is converted into a stream of bytes.
When writing to a file, we use the output stream, and when reading from a file, we use the input
stream. We use the System.IO namespace to access static methods for file handling.
These methods and properties are only declared and have no definition or fields as these are
defined within the class that the abstract class interface inherits. It is best practice to name an
interface class with ‘I’ to identify it as an interface visually.
The struct is a value type data type in C# that inherits from System.Value. These are used to
store data structures and usually small quantities of data. Structs can use parameterized
constructors, and they are declared with the struct keyword.
You can use the new keyword to create a struct, which will require parameters to be passed (if
defined in the constructor), else you can create a struct without the new keyword, which will
leave any struct members unassigned.
Struct Class
Value Types that are allocated on the stack or Reference Types that are allocated on the
on inline (more performant) heap and garbage-collected
Used for small data structures Used for complex data structures
Create with or without the new keyword Create with the new keyword
Cannot inherit from another class or struct Can inherit from another class or be the base
for another class to inherit from
Assignment to variables creates a copy of the Assignment to variables creates a copy of the
data, and operations on one variable will not object reference, and any operation on one
affect others variable will affect others
Want to become a C# developer? Check out the Best C# Courses to boost your skills and
enhance your resume.
Note: This is one of the most common C# .Net interview questions, so be sure to know the
difference between managed and unmanaged code.
These are used when we want to extend the functionality of an original method from a base
class. We use the virtual keyword to create a virtual method in the base class, which allows the
functionality to be overridden in the derived class. This is, therefore, an example of
polymorphism.
You cannot use the virtual keyword with static, abstract, or private methods in a base class.
A process for describing metadata for types, fields, and methods in your code at runtime by
using the System.Reflection namespace.
This can be useful to ‘reverse engineer’ your code after it executes, thus allowing you to
observe and examine important metadata for the different elements within your code.
A conversion process that changes a class object’s data into a byte stream. This can then be
transported over a network or stored in memory, a file, or a database.
Primarily, serialization is used to save an object’s state, allowing us to restore this later. To
reverse the process, we need to use deserialization. We have various serialization types
available to us.
● Binary: Convert object to binary bytes, useful for network transfer or local storage
● SOAP: Convert object to byte stream for network transfer over varied architectures
● XML: Convert object to XML format, useful for network transfer or local storage
● JSON: Convert object to a JSON string, useful for data exchange between apps
Each of the threads within the pool is designed for a particular task, and it waits in the pool until
that task type arises. Whenever a thread completes its task, it returns the pool to await future
assignments to new tasks.
No, because ‘this’ returns a reference to the current instance of the same class, and static
methods do not belong to any particular instance because they can be called by the class name
alone.
One exception is with Extension Methods, as we can use ‘this’ with the method's first parameter.
This is a way to share initialization code between different constructors within the same class or
from a base class via inheritance in the derived class.
If we have multiple constructors within the same class, we can chain these with the :this()
keyword, which allows us to flexibly implement various constructors within the same class.
If we want to chain constructors via inheritance, we can use the :base() keyword, which maps
the child class constructor to the constructor of the parent class.
35: Summarize the Thread Lifecycle in C#.
1. Unstarted State (New): New instance of the Thread class is initialized and not started
2. Runnable State: The Start() method is called, and the thread is ready to run
3. Running: A thread has been selected by the scheduler to run
4. Not Runnable: Sometimes referred to as WaitSleepJoin, a thread enters this state when
we call Wait() or Sleep() and also when it calls Join() on another thread
5. Dead State: A thread has completed its task, or the Abort() method has been called; this
is the last stage of a thread’s lifecycle
If we have a program that requires methods to be run independently of the primary process, we
need to use asynchronous programming. This allows us to run processes and, when needed,
make them wait without blocking the rest of the program.
To do this, we use the Async keyword to create an asynchronous method and Await to run it
without blocking our program.
Sometimes referred to as smart arrays, indexers are used to create a class or struct that
behaves like a virtual array.
By doing this, instances of the class or struct can be accessed using the standard array []
accessor, allowing them to be indexed like an element of an array.
38: What’s the Difference Between ‘ref’ and ‘out’ in C#?
Both the ref and out keywords allow us to pass in arguments by reference to a method. By
doing this, we can ensure that any changes we make to the argument within the body of the
method or function will be reflected in the original variable and maintained outside the method’s
scope.
● Ref: You must initialize parameters before passing to ref, but you don’t need to initialize
or assign the parameter before returning it to the calling method. This is often used to
pass arguments you want to modify in a function and maintain the changes.
● Out: You don’t need to initialize parameters before passing to out, but you do have to
ensure they are initialized before returning to the calling method. This is often used when
you have a method that you want to design to return multiple values.
Note this is one of those C# technical interview questions for experienced developers that you
should be able to answer without blinking.
A singleton is a special type of class (which also applies to other languages) that we use to
ensure that we can only create a single instance of the class. We also need to ensure that we
provide a global access point.
Logically, when using this design pattern, we don’t parameterize the constructor since we will
not create multiple class instances with different fields. We also ensure the constructor is
private.
Want to increase your chances of landing a C# developer job? Consider adding one of
the Best C# Certifications to your resume.
● Clarify Key Details: If you’re unsure about the interview structure, process, timing, or
anything else, contact the recruiter or hiring manager to ask any and all questions you
may have. You can also look on sites like Glassdoor for information from previous
interviews and candidates.
● Soft skills: Being prepared for all things technical is a strong starting point, but don’t be
caught off guard by questions relating to soft skills, including teamwork, communication,
dealing with difficult situations, etc. Generally, you should expect behavioral and
situational questions to test your soft skills and general personality.
Download our C# cheat sheet as a supplementary resource for your interview prep.
Conclusion
C# is a type-safe language that’s easy to learn and well-suited to object-oriented and
component-oriented programming with the .NET framework. While its roots are linked to the C
language family, coders with Java and Javascript familiarity will be comfortable with C# syntax.
This article has covered 40 C# interview questions and answers, with topics ranging from
complete beginner to C# interview questions for 5 years of experience or more. Any time you
can invest in studying these C# interview questions is time well spent to ensure you’re ready for
anything!
Interested in becoming a C# developer but want to boost your skills? Check out the
Complete C# Masterclass