I'm very curious to know why the delegate type is visible in intellisense when I type a dot after the class name in my program file and the delegate name appears after the class's object name.
Consider the following code
public class DelDec
{
public delegate void Initdel(String s);
public Initdel Init;
}
If I have a instancemethod in another class
public class Methoddef
{
public void process(string s)
{
Console.WriteLine("The message passed is" + s);
}
}
Now when
class Program
{
static void Main(string[] args)
{
Methoddef md=new Methoddef();
DelDec.Initdel d = md.process;
}
}
My concern is demonstrated with the following images
I really want to clarify this concept.
A delegate like this is just a nested class extending the special System.MulticastDelegate
type and with runtime defined methods. Have a look at the IL code, produced by compiling this c# code:
public class C {
public delegate void Del();
}
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Nested Types
.class nested public auto ansi sealed Del
extends [mscorlib]System.MulticastDelegate
{
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor (
object 'object',
native int 'method'
) runtime managed
{
} // end of method Del::.ctor
.method public hidebysig newslot virtual
instance void Invoke () runtime managed
{
} // end of method Del::Invoke
.method public hidebysig newslot virtual
instance class [mscorlib]System.IAsyncResult BeginInvoke (
class [mscorlib]System.AsyncCallback callback,
object 'object'
) runtime managed
{
} // end of method Del::BeginInvoke
.method public hidebysig newslot virtual
instance void EndInvoke (
class [mscorlib]System.IAsyncResult result
) runtime managed
{
} // end of method Del::EndInvoke
} // end of class Del
} // end of class C
Note the runtime
keyword at the method declaration, meaning that the method implementation will be provided by the runtime itself.
So what you are seeing in intellisense is just the presence of the nested type, the same should happen for any other nested type.
You declare method signature and name of delegate type, when you declare delegate in class. For example you know that you want call this method from you object with some signature, but you don't know which one, so you can it assign programmaticaly.
Here is code which can help you to understand.
class Program
{
static void Main(string[] args)
{
Methoddef md=new Methoddef();
DelDec.Initdel d = md.process; //you create variable of delegate type Initdel and asign this variable
var obj = new DelDec();
obj.Init = d; //assign delegate to class instance
obj.Init("Test");
}
public class DelDec
{
//Declares a delegate for a void method that takes an string parameter.
public delegate void Initdel(String s);
public Initdel Init;
}
public class Methoddef
{
public void process(string s)
{
Console.WriteLine("The message passed is " + s);
}
}
}
Also good if you read this https://msdn.microsoft.com/ru-ru/library/system.delegate(v=vs.110).aspx
So in your first picture DelDec.Initdel
- delagate type, in your second picrure obj.Initdel
it is field where you can assign come method to delegate.
I hope that my answer will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.