C # delegate parameter size

I was wondering if the C # delegates are taking up the same amount of space that C pointers (4 bytes) make when passing to the method.

Edit

Do delegates point to the correct methods only? they cannot point to structures or classes that I will fix.

+5
source share
1 answer

Yes, the delegate only points to methods, one or more. The parameters should be similar to the method.

public class Program
{
public delegate void Del(string message);
public delegate void Multiple();

public static void Main()
{
    Del handler = DelegateMethod;
    handler("Hello World");

    MethodWithCallback(5, 11, handler);

    Multiple multiplesMethods = MethodWithException;
    multiplesMethods += MethodOk;


    Console.WriteLine("Methods: " + multiplesMethods.GetInvocationList().GetLength(0));

    multiplesMethods();
}

public static void DelegateMethod(string message)
{
    Console.WriteLine(message);
}

public static void MethodWithCallback(int param1, int param2, Del callback)
{
    Console.WriteLine("The number is: " + (param1 + param2).ToString());
}

public static void MethodWithException()
{
    throw new Exception("Error");
}

public static void MethodOk()
{
    Console.WriteLine("Method OK!");

}

}

0
source

All Articles