Bcl (Base Class Library, 淌) now provides namespaces for System.Collections.Generic, including a few classes that define a model collection. The reason why it is called "generic" is because you just assign a type placeholder for the objects included, not to define it as a certain type. You can only specify a specific type when you create an instance of the collection. The generic saves our time. If you have created a custom collection, you will experience how much code you want to add. The generics in Visual Basic allows you to create a custom set match with only one line of code. You no longer have to give you a separate collection of each type of object you want to store. You only need to provide a type you like when you initialize a generic collection. If you want to know how generics work, the simplest way is to look at an example. Suppose you have a simple Customer class, which contains two disclosed properties (for simplicity, is defined below as a common variable): Public class customer public name as string public creditlimit as decimal public subnewe (Byval Customername As String , ByVal CustCreditLimit as Decimal) Name = CustomerName CreditLimit = CustCreditLimit End SubEnd class you can declare as a series of Customers like the following, which use to the List generic collection of class: Dim Customers as New System.Collections.Generic.List (of Customer Just this line of code, you have declared a strong type of list that can only store Customer types and gets complete intelligence awareness of the Customer object included. You can also create a series of Product or Order objects, and create a set of custom collections efficiently without having to fill in each type of code. You can write like this: Dim Custa As New Customer ("Custa", 1000) Customers.Add (Custa) Dim Custb As New Customer ("Custb", 2000) Customers.Add (Custb) for Each C As Customer In Customers MessageBox.show ("Customer:" & C.Name & ", Limit:" & c.creditlimit) Next 'Compile error: Product Type Unable to convert into Customer Type Dim Producta As New Product ("Proda", CDEC (29.95)) Customers.Add (Proda) The performance efficiency of generic collection is not inferior because the stored object is a strong type, not an introven to an Object type. Other model gates can be found under System.Collections.Generic namespace. For example, Dictionary (of k, v) collection allows you to limit the type of keyword and value. LINKEDLIST (Of T) and Stack (Of T) generic sets of generic sets and ordinary linked lists and stacks are not different, but you can specify the type of objects they want to store. You can use new generic statements to create your own generic.
Let's take a look at a COMPARER class, which can be used to compare two different types of objects: public class comparer (of itemtype) '... END CLASS You can define multiple types of places by using comma-separated lists symbol. You can also define a constraint condition to limit which classes can be provided to this generic after generic initialization. Public Class Comparer (of iTEMTYPE AS ICOMPARABLE) '... END CLASS This constraint condition ensures that COMPARER (OF T) instance can only be created through classes that implement the IComparable interface. Multi-constraints are also applicable to classes. As an example, let us look at this expanded, to achieve the Customer class IComparable interface: Public Class Customer Implements IComparablePublic Name As String Public CreditLimit As Decimal Public Sub New (ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName Creditlimit = CustcreditLimit End Sub
Public Function CompareTo (ByVal obj As Object) As Integer _ Implements IComparable.CompareTo Dim c As Customer = CType (obj, Customer) If CreditLimit> c.CreditLimit Then Return 1 If CreditLimit End Class Similarly, the Product class can also be implemented, but the Compareto function is compared to the product's price rather than the customer's deposit limit: Public Class Product Implements ICMPARABLIC NAME AS STRING PUBLIC Price As Decimal Public Sub New (...). .. Public Function CompareTo (...) ... End ClassComparer class provides generic comparison operations: Public class Comparer (of itemType As IComparable) Public Function GetLargest (ByVal Item1 As itemType, _ ByVal Item2 As itemType) As itemType Dim I as integer = item1.com, i> 0 Then Return Item1 if i <0 Then Return Item2 Return Nothing End Functionend Class You can now instantiate Comparers with different types of objects (there is nothing else here, Just say you can instantiate multiple Comparer instances, 淌 淌): DIM PC AS New Comparer (Of Product) Dim Product ("Littleone", 10) Dim Product2 AS New Product ("Bigone", 100) DIM LP As Product = Pc.getLargest (Prod1, Prod2) MessageBox.show ("The More Expensive Product IS:" & lp.Name) DIM CUS New Comparer ("Smallco", 1000 ) DIM CUST2 AS New Customer ("Largec O ", 5000) DIM LC AS Customer = cc.getlargest (Cust1, Cust2) MessageBox.show (" The customer with a higher limited is: "& lc.name) If there is no generic, you have to be any The object type you want to compare is defined for a comparison class (for example, ProductComparer and ORDERComparer). In a lot of common occasions, generics have largely alleviated your code burden. Imagine it, when you have a wide variety of classes, and they are only different on the object type of the operation, how easy it is to use the generic. @ Attach the following text for your reference @GenericsThe BCL now provides the System.Collections.Generic namespace, which contains several classes defining generic collections. They're called generic because at declaration you specify a type placeholder for the contained objects instead of a specific type. you give the stored objects a specific type only when you create an instance of the collection. Generics are a huge time saver. If you've ever created custom collections, you know how much code can be involved. Generics in Visual Basic let you create the equivalent of a custom collection in one line of code. you no longer need to create separate custom collections for each type of object you want to store. you simply provide the desired type as you instantiate the generic collection.The easiest way to see how generics work is with an example Assume you have a simple Customer class with two public properties (shown as public variables for brevity):. public class Customer public Name As String public CreditLimit As Decimal Public Sub New (ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName CreditLimit = CustCreditLimit End SubEnd ClassYou can declare a list of Customers using the generic collection List class like so: Dim Customers As New System.Collections.Generic. List (of customer) with this single line of cot, you ' ve declared a strongly typed list that stores only Customer types and gives you full IntelliSense on the contained Customer objects. You could just as easily create a list of Product or Order objects, effectively creating a set of custom collections without writing all the custom collection code For Each Type. You Can Now Write Code Like this: Dim Custa As New Customer ("Custa", 1000) Customers.Add (Custa) Dim Custb As New Customer ("Custb", 2000) Customers.Add (CUSTB) for Each c As Customer In Customers MessageBox.Show ( "Customer:" & c.Name & ", limit:" & c.CreditLimit) Next'compile error: Product can not be converted to CustomerDim prodA As New Product ( "ProdA", CDec ( 29.95)) Customers.Add (prodA) You also get a performance advantage using generic collections because the stored objects are strongly typed and not kept internally as type Object.There are other collection generics available in the System.Collections.Generic namespace. For example , The Dictionary (of K, V) Collection Allows You To SP ecify types for the keys and values. The LinkedList (of T) and Stack (of T) generic collections behave like regular linked lists and stacks, except you're allowed to specify what kinds of objects they'll contain.You can create your own generic types using the new generic type declaration syntax Consider a Comparer class that lets you compare different kinds of objects:. Public class Comparer (of itemType) '... End ClassYou can define multiple type placeholders in a comma-separated list you. Can Also Define Constraints Restricting Which Classes Can Be Provided To The Generic When it's Instantiated: Public Class Comparer (of itemType As IComparable) ' ... End ClassThis constraint ensures that Comparer (of T) instances can only be created with classes implementing the IComparable interface Multiple constraints can also be applied to the class declaration.As an example, consider an expanded Customer class that implements IComparable:. Public class Customer Implements IComparablePublic Name As String Public CreditLimit As Decimal Public Sub New (ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName CreditLimit = CustCreditLimit End Sub Public Function CompareTo (ByVal obj As Object) As Integer _ Implements IComparable.CompareTo Dim C as customer = ctype (obj, customer) if creditlimit> c.creditlimit the return 1 if Creditlimit