delegate new

In this chapter you will learn:

  1. Using new operator with delegate

Using new operator with delegate

using System;//from j a v  a  2s.c o m

class MainClass
{
  delegate int MyDelegate(string s);
  static void Main(string[] args)
  {
    MyDelegate Del1 = new MyDelegate(DoSomething);
    MyDelegate Del2 = new MyDelegate(DoSomething2);

    string MyString = "Hello World";

    Del1(MyString);
    Del2(MyString);

  }

  static int DoSomething(string s)
  {
    Console.WriteLine("DoSomething");
    
    return 0;
  }
  static int DoSomething2(string s)
  {
      Console.WriteLine("DoSomething2");
    return 0;
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use chained delegate
  2. Get Invocation List
Home » C# Tutorial » delegate, lambda, event
delegate
Multicast delegate
delegate variables
delegate parameters
Generic delegate
delegate return
Func and Action
delegate new
chained delegate
Anonymous delegate
delegate array
Return a delegate
delegate as parameter
Event
event multicast
static event
EventHandler
Event pattern
lambda
lambda syntax
Func, Action and lambda
lambda with outer function
lambda iteration variables