0

I just started learning delegates in c#. How the delegate object is created when the compiler encounter the following code

 delegate int Transformer (int x);
 Transformer t = new Transformer (Square);

I found that all the delegates implicitly derives the System.Delegate class. Will the "new Transformer (Square)" call the constructor of the Delegate class to create the object called 't'.

babybob
  • 444
  • 6
  • 22

1 Answers1

0

Looking back on Roslyn Reference Sources for .net framework 4.6.2 The System.Delegate class constructor looks like the following:

[System.Security.SecuritySafeCritical]  // auto-generated
        protected Delegate(Object target,String method)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (method == null)
                throw new ArgumentNullException("method");
            Contract.EndContractBlock();

            // This API existed in v1/v1.1 and only expected to create closed
            // instance delegates. Constrain the call to BindToMethodName to
            // such and don't allow relaxed signature matching (which could make
            // the choice of target method ambiguous) for backwards
            // compatibility. The name matching was case sensitive and we
            // preserve that as well.
            if (!BindToMethodName(target, (RuntimeType)target.GetType(), method,
                                  DelegateBindingFlags.InstanceMethodOnly |
                                  DelegateBindingFlags.ClosedDelegateOnly))
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
        }

        // This constructor is called from a class to generate a 
        // delegate based upon a static method name and the Type object
        // for the class defining the method.
        [System.Security.SecuritySafeCritical]  // auto-generated
        protected unsafe Delegate(Type target,String method)
        {
            if (target == null)
                throw new ArgumentNullException("target");

            if (target.IsGenericType && target.ContainsGenericParameters)
                throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), "target");

            if (method == null)
                throw new ArgumentNullException("method");
            Contract.EndContractBlock();

            RuntimeType rtTarget = target as RuntimeType;
            if (rtTarget == null)
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target");

            // This API existed in v1/v1.1 and only expected to create open
            // static delegates. Constrain the call to BindToMethodName to such
            // and don't allow relaxed signature matching (which could make the
            // choice of target method ambiguous) for backwards compatibility.
            // The name matching was case insensitive (no idea why this is
            // different from the constructor above) and we preserve that as
            // well.
            BindToMethodName(null, rtTarget, method,
                             DelegateBindingFlags.StaticMethodOnly |
                             DelegateBindingFlags.OpenDelegateOnly |
                             DelegateBindingFlags.CaselessMatching);
        }

So, yeah you can expect a derived type from this guy there.

If you're looking for basics on delegates: (I think I misread the question, still keeping it here)

Syntax for delegate declaration is

delegate <return type> <delegate-name> <parameter list>

From MSDN:

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

So, once a delegate type is declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.

public delegate void printStuff(string s);
...
printStuff ps1 = new printStuff(WriteToScreen);
printStuff ps2 = new printStuff(WriteToScreen);

WriteToScreen and WriteToScreen here are actually methods with a string in params.

Swagata Prateek
  • 1,076
  • 7
  • 15