HI WELCOME TO SIRIS

C# Interview Questions by topic part 2

Leave a Comment

Where did use delegates in your project - Part 1

Where did you use delegates in your project?
or
How did you use delegates in your project?
or
Usage of delegates in a Real Time Project?

This is a very common c sharp interview question. Delegates is one of the very important aspects to understand. Most of the interviewers ask you to explain the usage of delegates in a real time project that you have worked on. 

Delegates are extensively used by framework developers. Let us say we have a class called Employee as shown below.

Employee Class

The Employee class has the following properties.
1. Id
2. Name
3. Experience
4. Salary

Now, I want you to write a method in the Employee class, which can be used to promote employees. The method should take a list of Employee objects as a parameter, and should print the names of all the employees who are eligible for a promotion. But the logic, based on which the employee gets promoted should not be hard coded. At times, we may promote employees based on their experience and at times we may promote thembased on their salary or may be some other condition. So, the logic to promote employees should not be hard coded with in the method.

To achieve this, we can make use of delegates. So, now I would design my class as shown below. We also, created a delegate EligibleToPromote. This delegate takes Employee object as a parameter and returns a boolean. In the Employee class, we have PromoteEmpoloyee method. This method takes in a list of Employees and a Delegate of type EligibleToPromote as parameters. The method, then loops thru each employee object, and passes it to the delegate. If the delegate returns true, then them Employee is promoted, else not promoted. So, with in the method we have not hard coded any logic on how we want to promote employees.

What are the advantages of using interfaces



This is the most commonly asked interview question. This interview question is being asked in almost all the dot net interviews. It is very important that we understand all the concepts of interfaces and abstract classes.

Interfaces are very powerful. If properly used, interfaces provide all the advantages as listed below. 

1. Interfaces allow us to implement polymorphic behaviour. Ofcourse, abstract classes can also be used to implement polymorphic behaviour.

2. Interfaces allow us to develop very loosely coupled systems.
3. Interfaces enable mocking for better unit testing.

4. Interfaces enables us to implement multiple class inheritance in C#.

5. Interfaces are great for implementing Inverson of Control or Dependancy Injection.

6. Interfaces enable parallel application development.

C# Interview Questions on Delegates


What is a delegate?
delegate is a type safe function pointer. Using delegates you can pass methods as parameters. To pass a method as a parameter, to a delegate, the signature of the method must match the signature of the delegate. This is why, delegates are called type safe function pointers.

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

What do you mean by chaining delegates?
Or
What is a multicast delegate?
The capability of calling multiple methods on a single event is called as chaining delegates. Let me give you an example to understand this further.
1. Create a new asp.net web application
2. Drag and drop a button control and leave the ID as Button1. 
3. On the code behind file, add the code shown below.


When you click the Button now, both Method1 and Method2 will be executed. So, this capability of calling multiple methods on a single event is called as chaining delegates. In the example, we are using EventHandler delegate, to hook up Method1 and Method2 to the click event of the button control. Since, the EventHandler delegate is now pointing to multiple methods, it is also called as multicast delegate.

Will the following code compile?

No, the code does not compile. For the code to compile, the signature of Method1 should match the signature of SampleDelegate. 

Advantages and disadvantages of using generics in C#

To better understand the advantages and disadvantages of generics, it is better you read the below 2 articles first.

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 incur 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.

I cannot think of any disadvantages of using generics at the moment. Please feel free to comment if you are aware of any disadvantages.

The screen shot below shows, the generics collection classes and their respective non generic counterparts.

a

What are the advantages and disadvantages of using collection classes present in System.Collections namespace



We will understand the advantages and disadvantages of using collection classes present in System.Collections namespace, using ArrayList collection class. The same advantages and disadvantages apply to all other collection classes like Stack, Queue and Hashtable classes.


Advantages of using ArrayList:
1. ArrayList can grow in size dynamcally. In the example below, the Numbers ArrayList initial size is set 2. But we have added 3 elements. 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.



2. ArrayList provide several convinient 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 thier respective methods, to add or remove the elements.

Disadvantages of using ArrayList:
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.

Loosley typed collections can also cause performance overhead, 
because boxing and unboxing happens. In the example below, Numbers is an arraylist. We are stroing 10 and 20 which are integers and value types. Since, arraylist operate on object type, and object type is a 

reference type, the value 10 is boxed and converted into a reference type. The same is the case with integer 20. If we store 100 integers in the arraylist. All the 100 intgers 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 everytime we add and remove value types to the collection classes present in System.Collections namespace. This can severly affect the performance, especially if your collections are large. To solve this problem, we have generics introduced in dotnet. 




What are the advantages and disadvantages of using arrays



Advantages of using arrays:
1. Arrays are strongly typed, meaning you can only have one type of elements in the array. The strongly typed nature of arrays gives us 2 advantages. One, the performance will be much better because boxing and unboxing will not happen. Second, run time errors can be prevented because of type mis matches. Type mis matches and runtime errors are most commonly seen with collection classes like ArrayList, Queue, Stacketc, that are present in System.Collections namespace. 

In the example below, Numbers is an integer array. When we try to store a string in the integer array, a compiler error is reported stating cannot implicitly convert string to integer. This is why we call arrays are strongly typed.



In the example below
, Numbers is an ArrayList. Collections of type arraylist are loosely typed. This means any type of elements can be added to the collection. ArrayList operate on object type, which makes them 
loosely typed. No compiler error is reported, but when we run the application, a runtime error is reported as shown. In software development, it is always better to catch errors at compile time rather than at runtime.




Disadvantages of using arrays:
1. Arrays are fixed in size and cannot grow over time, where ArrayList in System.Collections namespace can grow dynamically.
2. Arrays are zero index based, and hence a little difficult to work with. The only way to store or retrieve elements from arrays, is to use integral index. Arrays donot provide convinient methods like Add(), Remove() etc provided by collection classes found in System.Collections or System.Collections.Generics namespaces, which are very easy to work with.

What are the advantages and disadvantages of using collection classes present in System.Collections namespace


We will understand the advantages and disadvantages of using collection classes present in System.Collections namespace, using ArrayList collection class. The same advantages and disadvantages apply to all other collection classes like Stack, Queue and Hashtable classes.


Advantages of using ArrayList:
1. ArrayList can grow in size dynamcally. In the example below, the Numbers ArrayList initial size is set 2. But we have added 3 elements. 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.




2. ArrayList provide several convinient 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 thier respective methods, to add or remove the elements.

Disadvantages of using ArrayList:
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.

Loosley typed collections can also cause performance overhead, 
because boxing and unboxing happens. In the example below, Numbers is an arraylist. We are stroing 10 and 20 which are integers and value types. Since, arraylist operate on object type, and object type is a 

reference type, the value 10 is boxed and converted into a reference type. The same is the case with integer 20. If we store 100 integers in the arraylist. All the 100 intgers 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 everytime we add and remove value types to the collection classes present in System.Collections namespace. This can severly affect the performance, especially if your collections are large. To solve this problem, we have generics introduced in dotnet. 


Remoting - Interview Questions


What is .NET Remoting?
.NET Remoting allows objects to interact with one another across application domains.


What are the 2 message encoding formats supported by .NET Remoting and when do you choose one over the other?
Message Encoding Formats:
1. Binary encoding.
2. XML encoding.


Applications can use binary encoding where performance is critical, or XML encoding where interoperability with other remoting frameworks is essential.


What are the two types of .NET remote objects?
1. Client-activated objects - Client-activated objects are under the control of a lease-based lifetime manager that ensures that the object is garbage collected when its lease expires.
2. Server-activated objects - In the case of server-activated objects, developers have a choice of selecting either a "single call" or "singleton" model. The lifetime of singletons are also controlled by lease-based lifetime.


What is considered as Remote Object?
Any object outside the application domain of the calling appication is considered remote object, even if the objects are executing on the same machine.

Can you treat every object as a remote object?
Objects that cannot be serialized cannot be passed to a different application domain and are therefore nonremotable.


What are the ways in which an object can be serialized?
1. Mark your class with serializable attribute.
2. Make your class implement ISerializable interface.


How can you change an object into a remote object?
Any object can be changed into a remote object by deriving it from MarshalByRefObject. 


What happens when a client activates a remote object?
When a client activates a remote object, it receives a proxy to the remote object. All operations on this proxy are appropriately indirected to enable the remoting infrastructure to intercept and forward the calls appropriately.


What are proxy objects and what is the use of these proxy objects?
Proxy objects are created when a client activates a remote object. The proxy object acts as a representative of the remote object and ensures that all calls made on the proxy are forwarded to the correct remote object instance.


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.