帮助里的:C# 中的委托类似于 C 或 C++ 中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与 C 或 C++ 中的函数指针不同,委托是面向对象、类型安全的,并且是安全的。委托声明定义一种类型,它用一组特定的参数以及返回类型封装方法。对于静态方法,委托对象封装要调用的方法。对于实例方法,委托对象同时封装一个实例和该实例上的一个方法。如果您有一个委托对象和一组适当的参数,则可以用这些参数调用该委托。委托的一个有趣且有用的属性是,它不知道或不关心自己引用的对象的类。任何对象都可以;只是方法的参数类型和返回类型必须与委托的参数类型和返回类型相匹配。这使得委托完全适合“匿名”调用。注意 委托是在调用方的安全权限下运行而不是声明方的权限下运行。在帮助里搜索"委托教程",里面有例子
C#中的委托和指针不一样,指针不通过MSIL而是直接和内存打交道,这也是指针不安全的原因所在,当然也是采用指针能够提高程序运行速度的缘故;委托不与内存打交道,而是把这一工作交给CLR去完成.
http://www.pconline.com.cn/pcedu/empolder/net/cs/10311/237203.html
Delegate Function Compare(ByVal x As Integer, _
ByVal y As Integer) As Boolean Function CompareValues(ByVal X As Integer, _
ByVal Y As Integer) As Boolean
If X > Y Then
CompareValues = True
Else
CompareValues = False
End If
End Function Sub SelectionSort(ByVal IsGreaterThan As Compare, _
ByVal IntArray() As Integer)
Dim MaxVal As Integer
Dim MaxIndex As Integer
Dim i, j As Integer ' Step through the elements in the array starting with the
' last element in the array.
For i = UBound(IntArray) To 1 Step -1
MaxVal = IntArray(i)
MaxIndex = i
For j = 1 To i
' Use the delegate to compare values.
If IsGreaterThan.Invoke(IntArray(j), MaxVal) Then
MaxVal = IntArray(j)
MaxIndex = j
End If
Next j
' Use the delegate to compare values.
If IsGreaterThan.Invoke(i, MaxIndex) Then
IntArray(MaxIndex) = IntArray(i)
IntArray(i) = MaxVal
End If
Next i
End Sub
End ClassClass Class1
Sub SortArray()
Dim Sort As New SortClass()
Dim arr1() As Integer = {1, 5, 3, 2, 7, 22, 5, 54, 12}
Sort.SelectionSort(AddressOf Sort.CompareValues, arr1)
MsgBox("Array sorted.")
End Sub
End Class' Add a button to your main form and insert the following code
' into the Click event handlr.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim c As New Class1()
c.SortArray()
End Sub