Part - 6 : Oops Interview Question's Answer




51. What is a multicast delegate?

It's a delegate that stores the address of multiple methods and eventually fires off several methods. Multicast delegate must have a return type of void.

52. What is an Asynchronous delegate?

When you invoke a delegate asynchronously, no new thread is created. Instead, the CLR automatically assigns a free thread from a small thread pool that it maintains. Typically, this thread pool starts with one thread and increases to a maximum of about 25 free threads on a single-CPU computer. As a result, if you start 50 asynchronous operations, one after the other, the first 25 will complete first. As soon as one ends, the freed thread is used to execute the next asynchronous operation.

53. What is mean by Events?

Events are nothing but a publisher and subscriber model. Any subscriber who is interested in receiving notification from the publisher can subscribe the events. If source event is fired or publisher raises the event, a notification will be send to all subscribers. One publisher can have multiple subscribers. Internally events will try to make use of delegate for this publisher, subscription model.
Example :
In the below example, we have created new event called "SampleEvent" and this event will be fired once MyMethod() is called. Anyone who wants to subscribe to this event can create a instance of the MyClassWithEvent and add handler to the event. So when ever event is raised, add handler method will be called.
Public Class MyClassWithEvent

  'Created New Event, which will return a message to all subscriber
   Event SampleEvent(ByVal message As String)

 'Event will be fired once this method is called
  Public Sub MyMethod()
    Console.WriteLine("MyMethod is called")
   'Raising the event with message
    RaiseEvent SampleEvent("Event is Raised from MyClassWithEvent")
  End Sub
End Class

Module Module1
 Sub Main()
  Dim c As New MyClassWithEvent
  'First subscriber of the event
   AddHandler c.SampleEvent, AddressOf EventSubscriber1
  'Second subscriber of the event
   AddHandler c.SampleEvent, AddressOf EventSubscriber2
   c.MyMethod()
   Console.ReadLine()
 End Sub

 Private Sub EventSubscriber1(ByVal message As String)
  Console.WriteLine("Subscriber 1")
  Console.WriteLine("Message: " + message)
 End Sub 

 Private Sub EventSubscriber2(ByVal message As String)
  Console.WriteLine("Subscriber 2")
  Console.WriteLine("Message: " + message)
 End Sub
End Module

OutPut :
======
   MyMethod is called
   Subscriber 1
   Message: Event is Raised from MyClassWithEvent
   Subscriber 2
   Message: Event is Raised from MyClassWithEvent


54. Can event's have access modifiers?

Yes, Event̢۪s can have access modifier, if we mention it as Protected events can be subscribed only within inherited class, If you mention it as Internal(C#)/Friends(VB.Net) it can be subscribed by all class inside the assembly. If you mention it as Private it can subscribed with in class where it is declared.

55. Can we have static/shared events?

Yes, we can have static(C#)/shared(VB.Net) event, but only shared method can raise shared events.

56. Can we have different access modifier for Get/Set of the properties?

Yes, in C# 3.0 and above, we can use different access modifier for Get/Set of the properties, but this is not possible in C#2.0 and lower

57. What is an indexer?

An indexer is an accessor that enables an object to be treated in the same way as an array. An indexer is considered when a class is better represented as a virtual container of data that can be retrieved or set using indices. Since an indexer is nameless, its signature is specified by the keyword "this" followed by its indexing parameters within square brackets.
Example :
In the below example we have created new index for class of type string. During get and set operation string manipulations are done.
public class MyClassForIndexer
{
  private string m_Name = "This is example for indexer";
  public string this[int index]
   { 
    get 
     {
       return m_Name.Substring( index);
     }
    set
     {
       m_Name = m_Name.Insert(index, value);
     }
   }
}

MyClassForIndexer ind = new MyClassForIndexer();
                        Console.WriteLine (ind[0]);
                        ind[7] = "Appended String";
                        Console.WriteLine(ind[0]);

Output :
======
   This is example for indexer
   This isAppended String example for indexer

58. What is ENUM?

ENUM means Enumeration; it is used to group related sets of constants. To create a enumeration you use the Enum statement Example :
Enum Months
    January = 1
    Feburary = 2
    March = 3
    April = 4
    May = 5
    June = 6
    July = 7
    August = 8
    September = 9
    October = 10
    November = 11
    December = 12
End Enum

Post a Comment

0 Comments