delegate is a function pointer : delegate « delegate « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » delegate » delegate 
9.1.6.delegate is a function pointer
using System;

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;
  }
}
DoSomething
DoSomething2
using System;

public class Machine
{
    string name;
    public Machine(string name)
    {
        this.name = name;
    }    
    public void Process(string message)
    {
        Console.WriteLine("{0}: {1}", name, message);
    }
}

class Test
{
    delegate void ProcessHandler(string message);
    
    static public void Process(string message)
    {
        Console.WriteLine("Test.Process(\"{0}\")", message);
    }
    public static void Main()
    {
        Machine computer = new Machine("Computer");
        
        ProcessHandler ph = new ProcessHandler(computer.Process);
        ph = (ProcessHandlerDelegate.Combine(ph, new ProcessHandler(Process));
        
        ph("compile");        
    }
}
Computer: compile
Test.Process("compile")
9.1.delegate
9.1.1.Define a delegate with no return value and no parameters
9.1.2.Add both static and non-static function to a delegate
9.1.3.Delegate with reference paramemters
9.1.4.Delegate with return values
9.1.5.Use a delegate to call object methods
9.1.6.delegate is a function pointer
9.1.7.A simple delegate example.
9.1.8.Construct a delegate using method group conversion
9.1.9.Delegates can refer to instance methods
9.1.10.Delegates to Instance Members
9.1.11.uses the invocation list to calculate a factorial
9.1.12.named-delegate invocation
9.1.13.Use delegate to reference two static functions
9.1.14.Declare a delegate and assigns a reference to either the WriteLine method or the ShowWindowsMessage method to its delegate instance.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.