Interview Question For C# Part 3

Thread Class

Use the Thread class for long-running tasks and when you require features offered by a formal threading model, e.g., choosing between foreground and background threads, tweaking the thread priority, fine-grained control over thread execution, etc.

How to marshal back the exception to main thread?

  1. Best bet is to replace the Thread with a Task (new in .NET 4.0). The Task class handles proper marshaling of the exception to whatever thread checks the result of the task.
  2. You can use the exception as a parameter in event.

And handle it after sending the exception to other thread.

  1. Dispatcher / SynchronisationContext is available

(ie. using WPF, Windows Forms, or a custom Dispatcher loop)

You can use ObserveOn + Catch to move the error back onto the Dispatcher thread. I’ve seen this used in a WPF application and it worked well.

  1. No Dispatcher available

Instead of using Console.ReadKey(), use a ManualResetEvent and wait on it, then throw the mutable error afterwards:

 

What is the difference between an Interface and an Abstract class?

There are quite a big difference between an interface and an abstract class, even though both look similar.

Interface definition begins with a keyword interface so it is of type interface.

Abstract classes are declared with the abstract keyword so it is of type class.

Interface has no implementation, but they have to be implemented.

Abstract class’s methods can have implementations and they have to be extended.

Interfaces can only have method declaration (implicitly public and abstract) and properties (implicitly public static).

Abstract class’s methods can’t have implementation only when declared abstract.

Interface can inherit more than one interfaces.

Abstract class can implement more than one interfaces, but can inherit only one class.

Abstract class must override all abstract method and may override virtual methods.

Abstract class can be used to provide some default behavior for a base class.

Interface makes implementation interchangeable.

Interface increase security by hiding the implementation.

Abstract class can be used when implementing framework.

Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies.

LEAVE A REPLY

Please enter your comment!
Please enter your name here