Define a delegate with no return value and no parameters : delegate « delegate « C# / CSharp Tutorial

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » delegate » delegate 
9. 1. 1. Define a delegate with no return value and no parameters
  1. A delegate is an object that can refer to a method.
  2. The method referenced by delegate can be called through the delegate.
  3. A delegate in C# is similar to a function pointer in C/C++.
  4. The same delegate can call different methods.
  5. A delegate is declared using the keyword delegate.

The general form of a delegate declaration:

delegate ret-type name(parameter-list);
  1. ret-type is return type.
  2. The parameters required by the methods are specified in the parameter-list.

A delegate can call only methods whose return type and parameter list match those specified by the delegate's declaration.

All delegates are classes that are implicitly derived from System.Delegate.

using System;


delegate void FunctionToCall();

class MyClass
{
   public void nonStaticMethod()
   {
      Console.WriteLine("nonStaticMethod");
   }

   public static void staticMethod()
   {
      Console.WriteLine("staticMethod");
   }
}

class MainClass
{
   static void Main()
   {
      MyClass t = new MyClass();    
      FunctionToCall functionDelegate;
      functionDelegate = t.nonStaticMethod;           

      functionDelegate += MyClass.staticMethod;
      functionDelegate += t.nonStaticMethod;
      functionDelegate += MyClass.staticMethod;

      functionDelegate();
   }
}
nonStaticMethod
staticMethod
nonStaticMethod
staticMethod
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
w_w_w___.___j__a_v__a2_s___.___c__o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.