This post contains typical interview questions and their answers related to C#. The answers are either created by myself according to the best of my understanding or taken from a website. In the latter case, references are cited. This post will be continuously improved.
Entry-Level
What is an object?
A class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection.
Source: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/objects
Define Constructor.
A constructor is a method whose name is the same as the name of the class. Whenever an instance of a class or a struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments.
Source: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors
Can “this” be used within a static method?
No, because ‘this’ returns a reference to the current instance of the class, and static methods do not belong to a particular instance.
What is the difference between out and ref parameters?
When an argument is passed as a ref, it must be initialized before it can be passed to the method. An out parameter, on the other hand, need not to be initialized before passing to a method.
Source: https://www.simplilearn.com/tutorials/c-sharp-tutorial/c-sharp-interview-questions
What is the benefit of ‘using’ statement in C#?
The ‘using’ statement can be used in order to obtain a resource for processing before automatically disposing it when execution is completed.
Source: https://www.simplilearn.com/tutorials/c-sharp-tutorial/c-sharp-interview-questions
What is serialization?
In order to transport an object through a network, we would need to convert it into a stream of bytes. This process is called Serialization.
Source: https://www.simplilearn.com/tutorials/c-sharp-tutorial/c-sharp-interview-questions
Differentiate between Break and Continue Statement.
Continue statement – Used in jumping over a particular iteration and getting into the next iteration of the loop.
Break statement – Used to skip the next statements of the current iteration and come out of the loop.
Source: https://www.simplilearn.com/tutorials/c-sharp-tutorial/c-sharp-interview-questions
What is Common Language Runtime (CLR)?
.NET provides a run-time environment called the common language runtime that runs the code and provides services that make the development process easier.
Compilers and tools expose the common language runtime’s functionality and enable you to write code that benefits from the managed execution environment.
The runtime automatically handles object layout and manages references to objects, releasing them when they’re no longer being used.
The common language runtime makes it easy to design components and applications whose objects interact across languages.
Source: https://learn.microsoft.com/en-us/dotnet/standard/clr
What is a managed and unmanaged code?
Code that you develop with a language compiler that targets the runtime is called managed code. Managed code benefits from features such as cross-language integration, cross-language exception handling, enhanced security, versioning and deployment support, a simplified model for component interaction, and debugging and profiling services.
Source: https://learn.microsoft.com/en-us/dotnet/standard/clr
Unmanaged code is when the code doesn’t run on CLR, it is an unmanaged code that works outside the .NET framework.
They don’t provide services of the high-level languages and therefore, run without them. Such an example is C++.
Source: https://www.interviewbit.com/c-sharp-interview-questions/
What is garbage collection in C#?
Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the heap memory. Now, after you perform all the actions on the object, the memory space occupied by the object becomes waste. It is necessary to free up memory.
Source: https://www.interviewbit.com/c-sharp-interview-questions/
What are the types of classes in C#?
Static class
A static class is sealed and therefore cannot be inherited; contains only static members and methods; cannot contain Instance Constructors; cannot be instantiated (you cannot use the new operator to create a variable of the class type).
A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, the static System.Math class in the .NET Class Library.
Partial class
Splitting the definition of a class, a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
Abstract Class
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own. Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.
abstract class Shape
{
public abstract int GetArea();
}
class Square : Shape
{
private int _side;
public Square(int n) => _side = n;
// GetArea method is required to avoid a compile-time error.
public override int GetArea() => _side * _side;
static void Main()
{
var sq = new Square(12);
Console.WriteLine($"Area of the square = {sq.GetArea()}");
}
}
// Output: Area of the square = 144
Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract
Sealed Class
When applied to a class, the sealed modifier prevents other classes from inheriting from it.
Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed
Is it possible to modify an abstract class with the sealed modifier?
No, it’s not possible. The two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract
What is the difference between an abstract class and an interface?
- Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods.
- Abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.
- Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.
Source: https://www.interviewbit.com/c-sharp-interview-questions/
Name all the C# access modifiers.
- Private Access Modifier – A private attribute or method is one that can only be accessed from within the class.
- Public Access Modifier – When an attribute or method is declared public, it can be accessed from anywhere in the code.
- Internal Access Modifier – When a property or method is defined as internal, it can only be accessible from the current assembly point of that class.
- Protected Access Modifier – When a user declares a method or attribute as protected, it can only be accessed by members of that class and those who inherit it.
Source: https://www.simplilearn.com/tutorials/c-sharp-tutorial/c-sharp-interview-questions