Delegate of C#.Net Interview Questions(10)
1.Define Delegate?
A) A delegate is a type that defines a method
signature. When you instantiate a delegate, you can associate its instance with
any method with a compatible signature. You can invoke (or call) the method
through the delegate instance.
2.Is delegate is a
member of a class?
A)It’s not a member of a
class but similar to a class.
3.List the properties of
an delegate?
A)Delegates have the
following properties:
Ø Delegates are like C++ function pointers but are
type safe.
Ø Delegates allow methods to be passed as
parameters.
Ø Delegates can be used to define callback
methods.
Ø Delegates can be chained together; for example,
multiple methods can be called on a single event.
Ø Methods do not have to match the delegate
signature exactly
Ø C# version 2.0 introduced the concept of
Anonymous Methods, which allow code blocks to be passed as parameters in place
of a separately defined method. C# 3.0 introduced lambda expressions as a more
concise way of writing inline code blocks.
4.How many types of delegates
are there?
A)There are two types of
delegates.They are:
Ø Single cast delegate
Ø Multi cast delegate
5.Define Single cast
delegate?
A)A delegate that
represents only a single function is known as Single Cast Delegate.
6.Define Multi cast
delegate?
A)A delegate that
represents only a more than one functions is known as Multi Cast Delegate.
7.Is Delegate supports
generics?
A)Delegate will support
generics.
8.Delegate belongs which
type?
A)Delegate is a type
that references a method.
9.How to declare a
delegate?
A)To implement a
delegate is a four step process declare,create,point and invoke.
10.What is the use of
delegates?
A)There are 6 important
uses of delegates.They are:
Ø Abstract
and encapsulate a method (Anonymous invocation):This is the most important use of delegates; it
helps us to define an abstract pointer which can point to methods and
functions. The same abstract delegate can be later used to point to that type
of functions and methods. In the previous section we have shown a simple example
of a maths class. Later addition of new algorithm functions does not affect the
UI code.
Ø Callback
mechanism:Many times we would like
to provide a call back mechanism. Delegates can be passed to the destination
and destination can use the same delegate pointer to make callbacks.
Ø Asynchronous
processing:By using ‘BeginInvoke’
and ‘EndInvoke’ we can call delegates asynchronously. In our previous section
we have explained the same in detail.
Ø Multicasting: Sequential processing Some time we would like
to call some methods in a sequential manner which can be done by using
multicast delegate. This is already explained in the multicast example shown
above.
Ø Events:Publisher subscriber modelWe can use events to
create a pure publisher / subscriber model.
0 comments:
Post a Comment