gpt4 book ai didi

VB.NET 为什么要这样声明这个子例程?

转载 作者:行者123 更新时间:2023-12-02 06:54:05 25 4
gpt4 key购买 nike

VB.NET 2010、.NET 4

我有一个基本问题:我有一个在网上某处找到的子例程,如此声明:

Public Sub MyFunction(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T)) ...

我想知道子名称后面声明的(Of T As Control)部分。我看到稍后在指定 Control 的类型和 Action(Of T) 中使用了 T,但为什么这样做而不是仅仅这样做:

Public Sub MyFunction(ByVal Control As Control, ByVal Action As Action(Of Control)) ...

潜艇名字后面的那部分是什么意思?它的目的是什么?非常感谢,并对我的无知感到抱歉。

最佳答案

那就是VB.NET的generic method声明语法:

A generic type is a single programming element that adapts to perform the same functionality for a variety of data types. When you define a generic class or procedure, you do not have to define a separate version for each data type for which you might want to perform that functionality.

An analogy is a screwdriver set with removable heads. You inspect the screw you need to turn and select the correct head for that screw (slotted, crossed, starred). Once you insert the correct head in the screwdriver handle, you perform the exact same function with the screwdriver, namely turning the screw.

关于VB.NET 为什么要这样声明这个子例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4262787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com