   [.net] Delegates
  How come in both the following examples, we never actually create a delegate? I always thought that in c#, everything derives from an Object, and unless is a value type, needs to be newed();for example, the below, how come, i can invoke the delegate? without actually making a delegate with new(). Im just use to newing all objects, and dont see how you can call a member function of an object, in this case the deletgate, without "making" it firstnamespace ProcessManipulator{    public delegate int Func(int num);    class Program    {   static void Main(string[] args)   {  run(MyFunc);   }   static void run(Func theDel)   {  theDel.Invoke(6);    }   static int MyFunc(int num)   {  return 6;   }    }}The same for the following, the code uses the "+=" to combine another delegate. But combine to what? how isnt the delegate "made" first? it looks as though it would just be a null object.namespace CarDelegate{  public class Car  {    // Define the delegate types.    public delegate void AboutToBlow(string msg);    public delegate void Exploded(string msg);    // Define member variables of each delegate.private AboutToBlow almostDeadList;    private Exploded explodedList;    // Add member to the invocation list.    public void OnAboutToBlow(AboutToBlow clientMethod)    { almostDeadList += clientMethod; }    public void OnExploded(Exploded clientMethod)    { explodedList += clientMethod; }    // Remove member from the invocation list.    public void RemoveAboutToBlow(AboutToBlow clientMethod)    { almostDeadList -= clientMethod; }    public void RemoveExploded(Exploded clientMethod)    { explodedList -= clientMethod; }  }}
Quote:Original post by maya18222How come in both the following examples, we never actually create a delegate? I always thought that in c#, everything derives from an Object, and unless is a value type, needs to be newed();string foo = "How now brown cow?";How come you didn't have to create the string? It's a reference type...(IIRC:)But the long answer is that in C# 1, you did need to. Everyone found it annoying to wrap the method in the delegate name, so in C# 2.0 they added the ability for auto-conversion from the raw method to the delegate type.;  Its just syntactical sugar. Same goes for "params" (which actually uses an array), pretty much anything involving strings (strings are immutable objects), and probably some others. You can, if you want, still use new. But there is a lot about C# that is just syntactical sugar. Why waste time reading/writing redundant stuff?If I remember correctly, this is just done by the C# compiler, not by the CIL. So if you use Reflector and disassemble your binary, you will see this stuff all added in.   ;  so its doing something like?namespace ProcessManipulator{    public delegate int Func(int num);    class Program    {   static void Main(string[] args)   {  run(new Func(MyFunc));   }   static void run(Func theDel)   {  theDel.Invoke(6);    }   static int MyFunc(int num)   {  return 6;   }    }};Quote:Original post by maya18222so its doing something like?*** Source Snippet Removed ***Yes.   