简体   繁体   中英

Why there are 2 () in a sub signature?

Recently I asked a question and got a working answer. The code below is the working code. I have problem understand the Sub signature. Why there are two ()() for the sub. I mean I don't understand the first one (Of Algorithm As SymmetricAlgorithm) . Obviously the second one is for the paramater. Can you point to me where I can read more about it?

Public Shared Sub DecryptTo(Of Algorithm As SymmetricAlgorithm)(sourceStream As Stream, stream As Stream, password As String)
    Dim pdb = GetPassword(password)
    Using alg = Activator.CreateInstance(Of Algorithm)()
        Using trans = alg.CreateDecryptor(pdb.GetBytes(alg.KeySize / 8), pdb.GetBytes(16))
            Using cStream = New CryptoStream(sourceStream, trans, CryptoStreamMode.Read)
                cStream.CopyTo(stream)
            End Using
        End Using
    End Using
End Sub

At http://msdn.microsoft.com/en-us/library/w256ka79(v=vs.80).aspx one can find a description of Generics (which is the reason for the first set of parentheses).

It starts:

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.

Screwdriver set as a generic tool

When you define a generic type, you parameterize it with one or more data types. This allows the using code to tailor the data types to its requirements. Your code can declare several different programming elements from the generic element, each one acting on a different set of data types. But the declared elements all perform the identical logic, no matter what data types they are using.

For example, you might want to create and use a queue class that operates on a specific data type such as String. You can declare such a class from System.Collections.Generic.Queue, as the following example shows.

VB Public stringQ As New System.Collections.Generic.Queue(Of String)

You can now use stringQ to work exclusively with String values. Because stringQ is specific for String instead of being generalized for Object values, you do not have late binding or type conversion. This saves execution time and reduces run-time errors.

VB.NET uses parenthesis in several different ways:

  • parameter section of method declaration or method calls (same as C#)
  • declaring generics (equivalent of <> in C#)
  • declaring and indexing arrays (equivalent of [] in C#)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM