Saturday 20 September 2014

What Is Delegate

Here I will explain what is Delegate and types of Delegate.
  1. Single Cast
  2. Multi Cast
  3. Asynchronous
Delegate is a type known as pointer type looks like method and perform like type derived from super type delegate.Delegate is Reliable declaring N number methods with same nature with different different operation can perform nay effective order through delegateDelegates can be various type

  1. Single Cast Delegate

It present at any time delegate contains address at one method like pointer.delegate can be void type or any method type return type.
Delegate with void type:
 Example
Under Page-Load:
abc1 ob= new abc1(abc);
ob.Ivoke();
}
void abc();
{
response.write("Ashok");
}
delegate void abc1();

Delegate with parameter:
 Example
Under page load:

abc1 ob= new abc1(abc);
ob.Ivoke("Ashok Mishra......");
}
void abc(string data);
{
response.write("Welcome:"+data);
}
delegate void abc1(string q);

Delegate with return type : Delegate should be same return type and same parameter type as it contain method address.
Example
 Under Page load:
loginref ob=new login ref(login);
string result=ob.Invoke("a""b");
Response.write("<hq>+result");
}
string login(string uid,string pwd)
{
if(uid=="a"&&pwd=="b")
{
return"ok"
}
else
{
return"try once........";
}
}
delegate string loginref (string a,string b);

Delegate with reference type:
Page Load:
{
msgref reft;
bool b=true;
if(b== false)
{
reft=new msgref(msg);
reft.Invoke();
}
else
{
reft=new msgref(msg 1);
reft.Invoke();
}
void msg()
{
Response.write("have....");
}
void msg1()
{
Response.write("*****")};
}
delegate void msgref();

Multicast Delegate
Delegate can be multicast. In multicast delegate it contain address of many delegate and execute method in sequence order can be used in various case like.........
Delegate contain operator like
1.Addition(Relates with "add" from delegate collection)
2. Subtraction(relates with "Remove" from delegate collection)
Example:
Under Page-Load:
sendmsg ob=new sendmsg(open);
sendmsg ob1=new sendmsg(read);
sendmsg ob2=new sendmsg(send);
sendmsg ob4=(sendmsg) sendmsg.combine(ob,ob1,ob2);
ob4.invoke();
response.write("<h1>");
response.write("a");
response.write("b");
response.write("c");
}
delegate void sendmsg();
string a,b,c;
void open()
{
a="open file";
}
void read()
{
b="read data from file";
}
void send()
{
c="send data";
}
}
 Asynchronous Delegate
It is relates with event handlers and multi thread with respective call back
Example;
Under page-Load;
{
Thread th =new Thread(disp);
th.start();
}
void disp()
{
responser.write("ashok");
}

0 comments::

Post a Comment

Copyright © DotNet-Ashok