Using Action(Of T...) over a custom delegate syntax for readability
Since .net 4.0, is the new ettiquette to use syntax such as:
Private Sub Main()
MyMethod(AddressOf AnAction)
End Sub
Private Sub MyMethod(ByVal toDo As Action(Of String, Integer, Boolean))
toDo.Invoke("Tom", 1, True)
End Sub
Private Sub AnAction(ByVal p1 As Stri开发者_运维知识库ng, ByVal p2 As Integer, ByVal p3 As Boolean)
End Sub
As opposed to:
Private Delegate Sub MyActionDlg(ByVal p1 As String, ByVal p2 As Integer, ByVal p3 As Boolean)
Private Sub Main()
MyMethod(AddressOf AnAction)
End Sub
Private Sub MyMethod(ByVal toDo As MyActionDlg)
toDo.Invoke("Tom", 1, True)
End Sub
Private Sub AnAction(ByVal p1 As String, ByVal p2 As Integer, ByVal p3 As Boolean)
End Sub
This goes the same with Tuples/Func too. It is nice to write but feels a bit sloppy in places. Why would anyone want Tuple(Of T, T2, T3, T4) over a nice class type? My actual piece of code has these nested, so it passes the Action along in a chain of methods.
Sorry for the VB example, AddressOf is just VBs way of saying new Action(AnAction)
. Also I just wrote this into the browser so it might not compile either.
How to people feel about reading and using these compiler generic actions as opposed to proper special entities crafted for their exact requirements in the solution?
The new Func
and Action
delegates are preferred for simple delegates like the ones sed in LINQ methods.
If using Func
s or Action
s would result in unreadable typenames, you certainly should define your own delegates.
In general, you should choose the design that results in the most readable code.
精彩评论