C# Interview Questions (part-2)

Explain object pool in C#?

Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.

What is static constructor?

Static constructor is used to initialize static data members as soon as the class is referenced first time.

Difference between this and base ?

"this" represents the current class instance while "base" the parent.

What's the base class of all exception classes?

System.Exception class is the base class for all exceptions.

How the exception handling is done in C#?

In C# there is a "try… catch" block to handle the exceptions.

Why to use "using" in C#?

The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens. Using calls Dispose() after the using-block is left, even if the code throws an exception.

What is a collection?

A collection works as a container for instances of other classes. All classes implement ICollection interface.

Can finally block be used without catch?

Yes, it is possible to have try block without catch block by using finally block. The "using" statement is equivalent try-finally.

How do you initiate a string without escaping each backslash?

You put an @ sign in front of the double-quoted string.

String ex = @"without escaping each backslash\r\n"

Can we execute multiple catch blocks in C#?

No. Once any exception is occurred it executes specific exception catch block and the control comes out.

What does the term immutable mean?

The data value may not be changed.

What is Reflection?

Reflection allows us to get metadata and assemblies of an object at runtime.

What namespaces are necessary to create a localized application?

System.Globalization, System.Resources.

Can you return multiple values from a function in C#?

Yes, you can return multiple values from a function using the following approaches:

  1. ref / out parameters
  2. Struct / Class
  3. Tuple

What is the difference between ref and out parameters?

"ref" tells the compiler that the object is initialized before entering the function, while "out" tells the compiler that the object will be initialized inside the function.

int x; disp(out x); // OK: you can pass without initialize
int y; disp(ref y); // Error: y should be initialized before calling the method

Differences | Value Types and Reference Types?

A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. That means, Value type holds some value only and not hold memory addresses and Reference types holds a memory address of a value. More about.... value types and reference types

Differences | constant and readonly variables in C#?

  1. Can be assigned values only at the time of declaration
  2. Static by default
  3. Known at compile time
Read only
  1. Must have set value, by the time constructor exits
  2. Are evaluated when instance is created
  3. Known at run time

Mention the assembly name where System namespace lies in?


What is the .NET datatype that allows the retrieval of data by a unique key?


What is Global Assembly Cache (GAC)? , and where is it located?

The Global Assembly Cache (GAC) is a folder in Windows directory to store the .NET assemblies that are specifically designated to be shared by all applications executed on a system.

By default you could see all assemblies installed in GAC in

c:\windir\assembly folder

Which method do you use to enforce garbage collection in .NET?

System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if situations arise.

See more.... How to force Garbage Collection See more... What is Garbage Collection

Why do I get errors when I try to serialize a Hashtable?

XmlSerializer will refuse to serialize instances of any class that implements IDictionary.

How can I stop my code being reverse-engineered from IL?

Obfuscate your code. Dotfuscator has a free edition and comes with Visual Studio. These tools work by "optimising" the IL in such a way that reverse-engineering becomes much more difficult. Also host your service in any cloud service provider that will protect your IL from reverse-engineering.

What is the use of Configuration Files?

The configuration file is really only for settings configured at deployment time. It can be used to deal with versioning issues with .NET components . And it's often used for connections strings - it's useful to be able to deploy an application to connect to a test or staging server, but this is not something you'd normally change in production once the application is deployed.

How Reference types and Value types are stored in memory ?

Value types get stored on stack and shared the same process memory, however the Reference Types get stored on Heap and their memory address value gets stored on the stack as value type.

How do you convert a string into an integer in .NET?


What is a formatter?

A formatter is an object that is responsible for encoding and serializing data into messages on one end, and deserializing and decoding messages into data on the other end.

How many classes can a single .NET DLL contain?

One DLL can contains Unlimited classes.

What is manifest?

It is the metadata that describes the assemblies

Difference between int and int32 ?

Both are same. System.Int32 is a .NET class. Int is an alias name for System.Int32.