Some More C# Interview Questions

What is static constructor?

Static constructor is used to initialize static data members as soon as the class is referenced first time, whereas an instance constructor is used to create an instance of that class with keyword. A static constructor does not take access modifiers or have parameters and can’t access any non-static data member of a class.

What is the use of Monitor in C#?

It provides a mechanism that synchronizes access to objects. 

The Monitor class controls access to objects by granting a lock for an object to a single thread. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object, no other thread can acquire that lock. You can also use Monitor to ensure that no other thread is allowed to access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object. 


What is lock statement in C#?

Lock ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread attempts to enter a locked code, it will wait, block, until the object is released.


How to loop through all rows of the DataTable?

You can do this in more than one way but ForEach loop is much better than any other way in terms of cleanliness of the code or performance. 

ForEach loop 
foreach (DataRow row in dTable.Rows)

{

     yourvariable = row[“ColumnName”].ToString();

}


For loop

for (int j = 0; j< dTable.Rows.Count; j++)

{

    yourvariable = dTable.Rows[j][“ColumnName”].ToString()l

}


What is an Array?

An array is a collection of related instance either value or reference types. Array posses an immutable structure in which the number of dimensions and size of the array are fixed at instantiation. 

C# Supports Single, Multi dimensional and Jagged Array

Single Dimensional Array: it is sometimes called vector array consists of single row. 

Multi-Dimensional Array: are rectangular & consists of rows and columns. 

Jagged Array: also consists of rows & columns but in irregular shaped (like row 1 has 3 column and row 2 has 5 column)

What is an ArrayList?

ArrayList is a dynamic array. Elements can be added & removed from an arraylist at the runtime. In this elements are not automatically sorted.

What is BitArray?

The BitArray collection is a composite of bit values. It stores 1 or 0 where 1 is true and 0 is false. This collection provides an efficient means of storing and retrieving bit values.

What is HashTable?

A Hashtable is a collection of key-value pairs. Entries in this are instance of DictionaryEntry type. It implements IDictionary, ISerilizable, IDeserializable collback interface.


 
What is Queue?

This is a collection that abstracts FIFO (First In First Out) data structure. The initial capacity is 32 elements. It is ideal for messaging components.

What is Stack?

This is a collection that abstracts LIFO (Last In First Out) data structure in which initial capacity is 32.

What is SortedList?

This is a collection and it is a combination of key/value entries and an ArrayList collection. Where the collection is sorted by key.

What is Delegates?

A delegate in C# allows you to pass method of one class to objects of other class that can call these methods. 

OR 

A delegate is an object that holds the reference to a method. 

In C++ it is called function pointer.

What is a collection?

A collection serves as a container for instances of other classes. All classes implement ICollection interface which intern implement IEnumerable interface.

What is reflection?

Reflection is the ability to find the information about types contained in an assembly at runtime. 

OR 

Reflection is the ability to find out information about objects, the application details (assemblies), its metadata at run-time. 

How is the DLL Hell problem solved in .NET?

Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.

Are private class-level variables inherited?

Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()?

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identacle object.

How can you sort the elements of the array in descending order?

By calling Array.Sort() and then Array.Reverse() methods.

What class is underneath the SortedList class?

A sorted HashTable.