Posts Tagged ‘collections’

C# Interview Question Part 1


1) What is a delegate?

Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method without having to know at compile time which method will be invoked. Delegates are object-oriented, type safe, and secure.

  1. A delegate represents a class.
  2. A delegate is type-safe.
  3. We can use delegates both for static and instance methods
  4. We can combine multiple delegates into a single delegate.
  5. Delegates are often used in event-based programming, such as publish/subscribe.
  6. We can use delegates in asynchronous-style programming.
  7. We can define delegates inside or outside of classes.

2)  What is the main use of delegates in C#?
Delegates are mainly used to define call back methods

3) What is a multicast delegate?
The capability of calling multiple methods on a single event is called as chaining/multicast
delegates

4)  What are the advantages of generics?

In Microsoft.NET version 1.0 there were collections, such as the ArrayList for working with groups of objects. An ArrayList is much like an array, except it could automatically grow and offered many convenience methods that arrays don’t have. The problem with ArrayList and all the other .NET v1.0 collections is that they operate on type object. Since all objects derive from the object type, you can assign anything to an ArrayList. The problem with this is that you acquire performance overhead converting value type objects to and from the object type and a single ArrayList could accidentally hold different types, which would cause a hard to find errors at runtime because you wrote code to work with one type. Generic collections fix these problems.

A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This eliminates type mismatches at runtime. Another benefit of type safety is that performance is better with value type objects because they don’t incur overhead of being converted to and from type object. With generic collections, you have the best of all worlds because they are strongly typed, like arrays, and you have the additional functionality, like ArrayList and other non-generic collections, without the problems.

It is always good to use generics rather than using ArrayList,Hashtable etc, found in System.Collections namespace. The only reason why you may want to use System.Collections is for backward compatibility

5) What are the advantages and
disadvantages of using Collection class present in System.Collections namespace?

Advantages

ArrayList can grow in size dynamically. This proves that ArrayList, and the rest of the collection classes like Stack, Queue and Hashtable can grow in size dynamically. If Numbers was an integer
array, then we would have run into Index out of Range compiler error.

ArrayList provide several convenient methods to add and remove elements to the collection. You can use the Add (), Remove () etc which are very handy to add and remove elements respectively. Similarly the Stack, Queue and Hashtable classes have their respective methods, to add or remove the elements.

Disadvantages

ArrayList and all other collection classes like stack, queue and hashtable which are present in System.Collection namespace operate on object and hence are loosely typed. The loosely typed
nature of these collections make them vulnerable to runtime errors.

Loosely typed collections can also cause performance overhead, because boxing and unboxing happens. In the example below, Numbers is an arraylist. We are storing 5 and 6 which are integers and value types. Since, arraylist operate on object type, and object type is a  reference type; the value 5 is boxed and converted into a reference type. The same is the case with integer 6. If we store 2000 integers in the arraylist. All the 2000 integers are boxed, meaning converted into reference types and then stored in the collection.

When we try to retrieve the elements out of the collection, we covert the object type back to integer type, unboxing happens. So this unnecessary boxing and unboxing happens behind the scenes every time we add and remove value types to the collection classes present in System.Collections namespace. This can severely affect the performance, especially if your collections are large. To solve this problem, we have generics introduced in dot net

6) What is the Difference between EXE and
DLL?

EXE is an executable file and can run by tself as an application; whereas DLL is usually consumed by an EXE or by another .DLL and we cannot run or execute DLL directly.

DLL’s can be reused, whereas EXE’s cannot be reused. Both EXE and DLL are called as assemblies.