[2022] C# Interview Questions – Frequently Asked

Written by

Vaaruni Agarwal


C# pronounced as C-Sharp is a programming language developed by Microsoft. It runs on the .NET Framework. C# can be used to develop web apps, desktop apps, mobile apps, games and much more. Anders Hejlsberg designed C #. It is one of the programming languages designed for the Common Language Infrastructure (CLI).

  1. Who designed C#?

Anders Hejlsberg designed C #

  1. Explain the features of C# in detail.


C# is a modern, general purpose and the object oriented programming language. It was developed by Microsoft and is designed for the Common Language Infrastructure (CLI). It consists of the executable code and runtime environment
that allows use of various high level languages such as JAVA, VB, etc. on different computer platforms and architectures.

  1. What is Managed and Unmanaged code in C#?

Managed code

: Executed by CLR (Common Language Runtime) which means, all application code is based on .Net platform. It is considered so, because of the .Net framework which internally uses the garbage collector to clean unused memory.

Unmanaged code:
 Executed by application runtime of any other framework other than .Net. The application runtime will take care of memory, security and other performance operations as well.

  1. What is an Interface?

An Interface is a class that has no implementation. The only thing that it contains is the declaration of the methods, properties, and corresponding events.

  1. What are the main reasons behind the usage of C# language?

C# has following features:

  • Easy to pickup
  • Component oriented language
  • Follows a Structured approach
  • Produces readable and efficient programs
  • Once written can be compiled on different platforms
  • Passing parameters is easy
  • Part of .NET framework.

  1. What are the different types of comments in C#?

There are three types of comments in C#:

 Single line comments

// hello, this is a single line comment on Studymite

Multiline comments

/* Hello this is a multiline comment

 last line of comment on Studymite*/

 XML comments

/// Hello this is XML comment on Studymite

  1. What access modifiers available in C#?

Public- Public attributes or methods can be accessed from any part of code.

Private– A private attribute or method is only accessible from within the class itself.

Protected– When a user defines a method or attribute as protected then it can be accessed only within that class and the one inheriting the class.

Internal– An internal method or internal attribute will be accessed from that class at the current assembly position.

Protected Internal
– An attribute or method is declared as protected internal, then it’s access is restricted to classes within the current project assembly or different types defined by that class.

  1. What do you understand by the public, static, and void keywords in C#?

Public declared variables or methods are accessible anywhere in the application.

Static declared variables or methods have a global access without creating an instance of the class.

The Void is a type modifier that states that the method or variable does not return any value.

  1. What is a Jagged Array in C#?

An array of arrays in known as the Jagged Array in C#. The elements can be of different dimensions and sizes.

  1. What is the difference between ref and out parameters in C#?

An argument passed as ref needs to be initialized before passing it to the method, whereas out
parameter must not be initialized before passing to a method.

  1. What is the use of ‘using’ statement in C#?

It is used to obtain a resource and process it simultaneously, then automatically dispose it of when the execution of the block is completed.

Eg:

        using system

  1. What is serialization?


When an object needs to be transported through a network, then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be
serializable, it should implement the ISerialize Interface.

  1. How is Exception Handling implemented in C#?

Exception handling is done using four keywords in C#:

try: Contains a block of code for which an expected exception will be checked.

catch: It is a piece of code that catches an exception with the help of the exception handler.

finally: It is a block of code written to execute regardless of whether an exception is caught or not.

Throw: Throws an exception whenever a problem occurs.

  1. What is Boxing and Unboxing in C#?

Boxing and Unboxing both are used for type conversions in C#.

 The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion.

// Boxing  in C#

int a = 12;  

Object o = a;  

Console.WriteLine(a);  

Console.WriteLine(o);

The reverse process of boxing is called unboxing.

// Unboxing  in C#

Object b = 1;  

int n = (int)b;  

Console.WriteLine(n);  

Console.WriteLine(b);

  1. What is the difference between Interface and Abstract Class in C#?

 A class can implement any number of interfaces but a subclass can implement only one abstract class.

An abstract class can have non-abstract methods (concrete methods) while in interface, all the methods must be abstract.

An abstract class can declare or use any variables while an interface is not allowed to do so.

In an abstract class, all data members or functions are private by default while in interface all are public.

An abstract class cannot be used for multiple inheritance while the interface can be.

  1. What is a sealed class in C#?

Sealed classes are created in situations when the class must be restricted from inheritance. For doing this the sealed
 modifier is used. Thus, a sealed class can never be a base class or a compilation error may occur.

  1. What is a Partial class in C#?

A partial class splits its definition of a class into multiple classes. They can either be in the same source code files or multiple source code files. One can create a class definition in multiple files but that is compiled as one
whole class at run-time. It is indicated by the keyword

partial.  

  1. What is the process of inheriting a class into another class?

Following syntax is used for inheriting classes:

public class Derivedclassname: baseclassname

{

//        methods and fields

.

.

.

}

  1. What is method overloading in C#?


Method overloading is creating multiple methods with the same name but with unique signatures within the same class. On compilation, the compiler makes use of overload resolution to determine that which method must be invoked.

  1. What are Custom Control and User Control?


Custom Controls are generated as compiled code (Dlls), these are easier to use and can be added to toolbox. Developers can drag and drop controls to their web forms. They can easily be added to Multiple Applications (If they Share
Dlls).

User Controls are similar to ASP include files, and are easy to create. They cannot be placed in the toolbox and dragged and dropped from it. They have their own design and code behind files.

  1. What are the differences between the classes represented by System.String and System.Text.StringBuilder?

System.String is immutable string class. That means on modification of the value of a string variable, a new memory is allocated to the new value and the previous memory allocation released.

System.StringBuilder on the other hand is mutable string class, where the operations can be performed without allocatimg separate memory location for the modified string.

  1. What’s the difference between the CopyTo() and Clone() methods in C#?

Both of the methods perform a shallow copy of the elements, while the Clone()
method, creates a new array object that contains all the original array elements. On the other hand, the CopyTo()
 method copies all the elements of the array into an already existing array.

  1. What is the difference between Finalize() and Dispose() methods?


Dispose() method is called when the object is required to release any unmanaged resources and ensures the garbage collection of an object as well. However, the, Finalize() method is used for the same purpose as well , but it does
not assures the garbage collection of an object.

  1. What are circular references in C#?

A Circular reference is a situation in which two or more resources are interdependent on each other, this kind of reference usually causes the lock condition and make the resources unusable by anyone.

  1. What is the purpose of is operator in C#?

is operator determines whether an object is of a certain type. On the basis of the condition it return a Boolean output.

Eg:

if( Mango is fruit) // checks if Mango is an object of the fruit class or not

  1. What is the purpose of as operator in C#?

as operator casts one data type to another without raising an exception, even if the cast fails.

Eg:

Object o = new StringReader(“Hello”);

StringReader r = o as StringReader;

  1. What is the difference between finally and finalize?

finally block is called after the execution of the try and catch block. It is used for exception handling and whether an exception is caught or not, the block of code written in finally will be executed.


finalize method is called just before the process of garbage collection. It is used to perform clean up operations of the Unmanaged code. It is automatically called when a given instance is not subsequently called.

  1. What is an Escape Sequence?

An Escape sequence is denoted by a backslash (\). The backslash indicates that the character that follows it should be interpreted as a special character. An escape sequence is considered as a single character.

String escape sequences are as follows:

\n – Newline character

\b – Backspace

\\ – Backslash

\’ – Single quote

\’’ – Double Quote

  1. What are Regular expressions?


Regular expression is a template to match a set of input. Whenever a user passes a data then that data must be validated first before treating it, thus regular expressions are used to perform validations over the data. The
pattern can consist of operators, constructs or character literals. Regex is used for string parsing and replacing the character string.

  1. What is a Delegate in C#? Explain.

A Delegate is a variable that holds the reference to a method or a function in C#. It is a function pointer or a reference type variable. All the Delegates are derived from System.Delegate
 namespace. Both the delegate and the method that it is associated with must have the same signature.

  1. What are Events?

Events are user actions that generate notifications to which the programs must respond. The user actions can be mouse movements, keypress events and many more.

A class that raises an event is called a publisher and a class which responds to that event is called a subscriber
. Event should have at least one subscriber or that event never gets raised.

  1. What is a Generic Class?


A Generic class also known as Generics is used to create classes or objects which do not have any specific data type. The data type can be assigned during runtime, that is when ever a generic class is used in the program. It can
work with any data type.

  1. What is IEnumerable<> in C#?

 IEnumerable is the parent interface for all the non generic collections available in System.Collections namespace. These may include ArrayLists, HastTables and many more.

 

  1. What is the difference between late binding and early binding in C#?

 Early Binding and Late Binding are the concepts of polymorphism in C#.

Compile Time Polymorphism or Early Binding:

In this, multiple methods are used with the same name but different types of parameters, or the different number of parameters. Thus different tasks with the same method name in the same class can be performed, known as Method
overloading.

 Run Time Polymorphism or Late Binding:

In this, same method names with the same signatures, which means the same type or the same number of parameters are used, but they need not be in the same class. Thus, this bind is used at run time in the derived class when an
object is instantiated.

  1. What is the lock statement in C#?


The Lock statement is used to ensure that one thread does not enter a critical section of code while another thread is already in the critical section. If another thread attempts to enter a locked code it would need to wait and
block, until the object is released.

  1. What is object pool in .Net?

An Object pool is a container that consists of ready to use objects. It reduces the overhead of creating a new object every time.

  1. What is a Hashtable in C#?

A Hashtable is a collection of the key and value pairs. It contains the values, which are based on the keys.

  1. What is Reflection?

Reflection allows users to get metadata and assemblies of an object, during the runtime.

  1. What is Garbage Collection?

Garbage Collection is the process of releasing the memory automatically, which is occupied by objects that are no longer accessible.

  1. What is the difference between the Virtual and the Abstract methods in C#?

Virtual methods have a default implementation. This implementation can be overridden in a derived class with the use of the keyword, override.

An Abstract method, on the other hand does not have any implementation. It resides in the abstract class, it is necessary that the derived class must implement abstract method.

  1. Define C# I/O classes.

C# consists of System.IO namespace. This namespace has classes that compute and perform various operations like creating, deleting, opening and closing the files.

  1. What is a thread in C#?


A Thread is a set of instructions which when executed enables the program to perform parallel processing. It helps in doing more than one process at a time. The threads can be created to execute the code in parallel with the
original thread.

  1. Explain thread life cycle in C#.

A Thread follows a life cycle where it starts whenever a thread is created and gets terminated immediately after the execution. The life cycle of a thread is as follows:

  • Start
  • Sleep
  • Abort
  • Suspend
  • Resume
  • Join
  1. What are the Synchronous and Asynchronous Operations in C#?


Synchronization creates a thread-safe code where only a single thread will access the code at a time. Synchronous call waits for completion of the method. While in an Asynchronous operation, the program is allowed to perform other
operations, while the current method called, completes its share of work.

  1. What are Custom Exceptions in C#?

Custom Exceptions are user-defined exceptions, there are various situations in which the errors must be handled as per the user requirements. In such cases the Custom Exceptions are called in C#.

  1. What is a nullable type in .NET?

The Value types can take either their normal values or even a null value. Such types are called nullable types.

Eg:

int? s = null;

if(s.HasVAlue)

{

        //statements

}

else

        {

}

  1. What is the use of Null Coalescing Operator (??) in C#?


The null coalescing operator is used with the nullable and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.

  1. What are indexers?

Indexers are also known as the smart arrays in C#. They allow the instances of a class to be indexed in the same way as an array.

Eg:

public int this[int index]    //indexer in C#

  1. What is a Console application?

An application that can run on the command prompt in Windows is known as the Console Application. It is the easiest application, and is thus preferred by the beginners.

  1. What is LINQ?

Language Integrated Query or LINQ is a data querying method that provides querying capabilities to .NET languages, like C#. It has a syntax similar to a SQL query.

 

[2022] C# Interview Questions &#8211; Frequently Asked