Delegate vs. delegate keyword

c# delegate
delegates and events in c# for beginners
c# delegate vs event
c# action
c# delegate invoke
c# delegates explained
advantages of delegates in c#
c# delegate vs func

If you like to create custom delegates you would use the delegate keyword in lowercase.

What can you do with the actual Delegate Class? What is this good for? I don't understand the exact difference.

From http://msdn.microsoft.com/en-us/library/system.delegate.aspx:

The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types.

Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.

Delegates types are declared with the delegate keyword. They can appear either on their own or nested within a class, as shown below. namespace  The compiler generates a class, derived from System.Delegate that matches the signature used (in this case, a method that returns an integer, and has two arguments). The type of that delegate is Comparison. The Comparison delegate type is a generic type. For details on generics see here.

Delegates. A delegate in C# is similar to a function pointer in C or C++. The keyword event controls how the event property is accessed by the subscribing  Delegate is an object used as a function pointer to hold the reference of a method. On the other hand, events provide an abstraction to delegates. A keyword required to declare a delegate is a delegate whereas, a keyword required to declare an event is event. A delegate is declared outside a class whereas, an event is declared inside a class.

Another neat thing you can do with delegate keyword is create delegates inline, without having to declare them, for example:

// constructor
public Form1()
{
    this.Load += delegate(object sender, EventArgs e)
    {
         // Form1_Load code goes right here
    }
}

Learn how you can take advantage of delegates like Action, Func, and Predicate to event handling, and they are declared using the “delegate” keyword. Func is a delegate that points to a method that accepts one or more  Delegates and lambdas. 06/20/2016; 4 minutes to read +9; In this article. Delegates define a type that specifies a particular method signature. A method (static or instance) that satisfies this signature can be assigned to a variable of that type, then called directly (with the appropriate arguments) or passed as an argument itself to another method and then called.

The advantage of the Delegate class is that it is the base class for all delegate types in .Net. Having a method which takes an instance of this class allows you to operate generically over all manner of delegates. This is the reason operations like ISynchronizedInvoke.Invoke use this as a parameter.

A multicast delegate is one that can point to one or more methods that event handling, and they are declared using the "delegate" keyword. Delegates with Named vs. Anonymous Methods (C# Programming Guide) 07/20/2015; 2 minutes to read +6; In this article. A delegate can be associated with a named method. When you instantiate a delegate by using a named method, the method is passed as a parameter, for example:

One of the things the Delegate class can be used for is more control when invoking event handlers. For example, with normal event processing, an exception in any event handler will prevent any later event handlers from being called. You can alter that behavior by using the Delegate class to manually invoke each event handler.

using System;

namespace DelegateClass
{
    class EventSource
    {
        public event EventHandler TheEvent;

        internal void RaiseEvent1()
        {
            EventHandler handler = TheEvent;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        internal void RaiseEvent2()
        {
            EventHandler handler = TheEvent;
            if (handler == null)
                return;

            Delegate[] handlers = handler.GetInvocationList();
            foreach (Delegate d in handlers)
            {
                object[] args = new object[] { this, EventArgs.Empty };
                try
                {
                    d.DynamicInvoke(args);
                }
                catch (Exception ex)
                {
                    while (ex.InnerException != null)
                        ex = ex.InnerException;

                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

    class Program
    {
        static void Handler1(object sender, EventArgs e)
        {
            Console.WriteLine("Handler1");
        }

        static void Handler2(object sender, EventArgs e)
        {
            Console.WriteLine("Handler2");
            throw new InvalidOperationException();
        }

        static void Handler3(object sender, EventArgs e)
        {
            Console.WriteLine("Handler3");
        }

        static void Main(string[] args)
        {
            EventSource source = new EventSource();
            source.TheEvent += Handler1;
            source.TheEvent += Handler2;
            source.TheEvent += Handler3;

            try
            {
                source.RaiseEvent1();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("-------------------");

            source.RaiseEvent2();
        }
    }
}

exactly like the declaration of a method, except with the keyword delegate in front of it. Examples: A delegate representing a method that adds two numbers and returns Should I Use Action or Func or Declare a delegate ? This delegate was introduced in Framework 2.0. We can now use Action<in T> delegate to pass a method as a parameter without explicitly defining the delegate. The compiler will take care of it. This delegate can accept parameters but without a return type. Call the delegate using act(4, 5) Func< T, TResult> This was introduced in Framework3.5.

How does C# handles the callback functions or event handler? A delegate can be declared using the delegate keyword followed by a  The difference between both is that Event wraps Delegate type, and makes Delegate not modifiable in terms of changing references, i.e. assigning a new object is not possible.

You can initialize an Action delegate using the new keyword or by directly assigning a method: Action<int> printActionDel = ConsolePrint; //Or Action<int>  Delegate declaration is similar to a method declaration but contains the keyword ‘delegate’.   The syntax of the delegate is as follows. delegate < return type> <delegate name> <parameter list>. The return type declares the return value by the method. It is called by the delegate.

The Delegate statement defines the parameter and return types of a delegate class. Any procedure with matching parameters and return types can be used to create an instance of this delegate class. The procedure can then later be invoked by means of the delegate instance, by calling the delegate's Invoke method.

Comments
  • +1: Specially to mention that "the compiler creates a new Type for you derived from MulticastDelegate". So by default, the delegates we create are all Multicast delegates
  • the compiler creates a new Type for you derived from MulticastDelegate I am not sure about this. According to msdn.microsoft.com/en-us/library/system.delegate.aspx However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. Please note the keyword "or"
  • The C# 3.0 lambda syntax is much less verbose.
  • It should be noted, however, that invoking a delegate in this manner is late-bound and (accordingly) slower.