As you can see, generics are easy to use. Strongly typed code can avoid run-time errors, and IntelliSense will work better. While there are good reasons to use generics, there are more advantages to using generics: Performance and code reuse.
The introduction of generic technology. NET Framework is one of the main reasons for improving performance. For example, a collection class can work faster than before because the compiler can optimize for the type that the collection stores. The following code compares the performance of arrays, ArrayList, and the generic list:
This code stores 5 million values in a fixed-length array, and also stores the same number of values in an automatically growing ArrayList and generic list, and the performance value looks interesting:
Array Time: 344
ArrayList time: 4656
List time: 797
There are certain types of fixed-length arrays that have unmatched speed and do not have to pay for changing size. The size of the collection type is automatically increased, and if there is a fixed array 1/2 performance is pretty good. Next look at ArrayList, very unfortunate, only fixed data 1/10 performance. The problem is that the ArrayList is designed to store reference variables, the integer is a value type, and a "boxing" operation is used to convert the integer to object type before it is stored in ArrayList. The cost of boxing is very expensive, so when you store value-type data (such as Integer, Date, Boolean, and structure you create yourself), using generics will achieve a significant performance boost.
For more information about the boxing and unboxing operations, see "Boxing conversions" and "unboxing Conversions" in the MSDN Library
Creating generic types and methods
Instead of using only generic types provided by Visual Basic.NET, you can create your own generic types and methods.
Generic method
When you want to implement some generic algorithms that are not related to a particular type, you may want to create a generic method. For example, a typical bubble sort needs to traverse all the items in an array, 22 comparisons, and exchange values that need to be sorted.
If you've already determined that you can simply write an integer, you might want to do a swap method that can only be used for an integer type. But if you want to be able to sort any type, you can write a generic swap method as follows:
Private Sub Swap (of ItemType) _
(ByRef v1 as ItemType, ByRef v2 as ItemType)
Dim Temp as ItemType
temp = V1
V1 = V2
V2 = Temp
End Sub
Note "of ItemType", when the Swap method is invoked, you must also pass in a data type in addition to the required parameters. This data type replaces the itemtype in any instance. The following example calls swap:
Swap (of Integer) (V1, v2)
This statement tells the Swap method that it will exchange an integer type. If you look back at swap code, the meaning of this statement is to have the JIT replace all the itemtype with integers, and this swap method has actually been rewritten by JIT:
Private Sub Swap (ByRef v1 As Integer, ByRef v2 As Integer)
Dim Temp as Integer
temp = V1
V1 = V2
V2 = Temp
End Sub
This is the code that is actually executed, and the JIT generates a method that is specific to the integer type. If you next want to sort the string type, you can use another swap call as follows:
Swap (of String) (V1, v2)
When the method executes, the JIT generates another version of Swap, which is specific to string type:
Private Sub Swap (ByRef v1 As String, ByRef v2 As String)
Dim Temp as String
temp = V1
V1 = V2
V2 = Temp
End Sub
The following is a complete example of a bubble sort using generic swap:
Private Sub Btnsortintegers_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles BTNSORTINTEGERS.CLI Ck
Dim INTs (9) as Integer
Dim R as New Random
For i as Integer = 0 to 9
INTs (i) = R.next (1, 100)
Next
' Bubble sort
For j as Integer = 0 to 9
For k as Integer = 9 to 1 Step-1
If INTs (k) < INTs (k-1) Then
Swap (of Integer) (INTs (k), INTs (k-1))
End If
Next
Next
Txtoutput.text = "Sort integers" & vbCrLf
For i as Integer = 0 to 9
Txtoutput.text &= ints (i) & VbCrLf
Next
End Sub
Generic type
Finally, you can create a fully generic type and use this "of ItemType" method to create the declaration of the class as follows:
Public Class SomeClass (of ItemType)
Private Internalvar as ItemType
Public Function SomeMethod (ByVal value as ItemType) as ItemType
End Function
End Class
This code works the same way as the class. The JIT compiler simply replaces the itemtype in the instance with the type specifically specified when instantiated.
Constraints
Generic technology also supports a feature called constraints. This feature ensures that at least some functionality is implemented for the type passed in when the type is specified. For example, if you want to implement a sort algorithm, you need to ensure that the incoming type implements the Icomparible interface. You can use constraints to accomplish this idea:
Public Class SomeClass (of ItemType as Icomparible)
Public Function SomeMethod (ByVal value as ItemType) as ItemType
End Function
End Class
Conclusion
Generic technology provides a number of benefits over an object based collection, and first, the generic class is strongly typed, ensuring that all errors are discovered at compile time. Strong typing can also make IntelliSense more convenient. Generics also allow you to simplify your code so that your algorithms can be used for a variety of types. Finally, a generic collection is much faster than an object based collection, especially when used for value types.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.