本文整理汇总了VB.NET中System.Collections.ObjectModel.Collection<T>类的典型用法代码示例。如果您正苦于以下问题:VB.NET Collection<T>类的具体用法?VB.NET Collection<T>怎么用?VB.NET Collection<T>使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection<T>类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Demo
' 导入命名空间
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Demo
Public Shared Sub Main()
Dim dinosaurs As New Collection(Of String)
dinosaurs.Add("Psitticosaurus")
dinosaurs.Add("Caudipteryx")
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Muttaburrasaurus")
Console.WriteLine("{0} dinosaurs:", dinosaurs.Count)
Display(dinosaurs)
Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
dinosaurs.IndexOf("Muttaburrasaurus"))
Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
dinosaurs.Contains("Caudipteryx"))
Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
dinosaurs.Insert(2, "Nanotyrannus")
Display(dinosaurs)
Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))
Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
dinosaurs(2) = "Microraptor"
Display(dinosaurs)
Console.WriteLine(vbLf & "Remove(""Microraptor"")")
dinosaurs.Remove("Microraptor")
Display(dinosaurs)
Console.WriteLine(vbLf & "RemoveAt(0)")
dinosaurs.RemoveAt(0)
Display(dinosaurs)
Console.WriteLine(vbLf & "dinosaurs.Clear()")
dinosaurs.Clear()
Console.WriteLine("Count: {0}", dinosaurs.Count)
End Sub
Private Shared Sub Display(ByVal cs As Collection(Of String))
Console.WriteLine()
For Each item As String In cs
Console.WriteLine(item)
Next item
End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Collections.ObjectModel,代码行数:55,代码来源:Collection 输出:
4 dinosaurs:
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
IndexOf("Muttaburrasaurus"): 3
Contains("Caudipteryx"): True
Insert(2, "Nanotyrannus")
Psitticosaurus
Caudipteryx
Nanotyrannus
Compsognathus
Muttaburrasaurus
dinosaurs(2): Nanotyrannus
dinosaurs(2) = "Microraptor"
Psitticosaurus
Caudipteryx
Microraptor
Compsognathus
Muttaburrasaurus
Remove("Microraptor")
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
RemoveAt(0)
Caudipteryx
Compsognathus
Muttaburrasaurus
dinosaurs.Clear()
Count: 0
示例2: Dinosaurs
' 导入命名空间
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Public Class Dinosaurs
Inherits Collection(Of String)
Public Event Changed As EventHandler(Of DinosaursChangedEventArgs)
Protected Overrides Sub InsertItem( _
ByVal index As Integer, ByVal newItem As String)
MyBase.InsertItem(index, newItem)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Added, newItem, Nothing))
End Sub
Protected Overrides Sub SetItem(ByVal index As Integer, _
ByVal newItem As String)
Dim replaced As String = Items(index)
MyBase.SetItem(index, newItem)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Replaced, replaced, newItem))
End Sub
Protected Overrides Sub RemoveItem(ByVal index As Integer)
Dim removedItem As String = Items(index)
MyBase.RemoveItem(index)
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Removed, removedItem, Nothing))
End Sub
Protected Overrides Sub ClearItems()
MyBase.ClearItems()
RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
ChangeType.Cleared, Nothing, Nothing))
End Sub
End Class
' Event argument for the Changed event.
'
Public Class DinosaursChangedEventArgs
Inherits EventArgs
Public ReadOnly ChangedItem As String
Public ReadOnly ChangeType As ChangeType
Public ReadOnly ReplacedWith As String
Public Sub New(ByVal change As ChangeType, ByVal item As String, _
ByVal replacement As String)
ChangeType = change
ChangedItem = item
ReplacedWith = replacement
End Sub
End Class
Public Enum ChangeType
Added
Removed
Replaced
Cleared
End Enum
Public Class Demo
Public Shared Sub Main()
Dim dinosaurs As New Dinosaurs
AddHandler dinosaurs.Changed, AddressOf ChangedHandler
dinosaurs.Add("Psitticosaurus")
dinosaurs.Add("Caudipteryx")
dinosaurs.Add("Compsognathus")
dinosaurs.Add("Muttaburrasaurus")
Display(dinosaurs)
Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _
dinosaurs.IndexOf("Muttaburrasaurus"))
Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _
dinosaurs.Contains("Caudipteryx"))
Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
dinosaurs.Insert(2, "Nanotyrannus")
Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))
Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
dinosaurs(2) = "Microraptor"
Console.WriteLine(vbLf & "Remove(""Microraptor"")")
dinosaurs.Remove("Microraptor")
Console.WriteLine(vbLf & "RemoveAt(0)")
dinosaurs.RemoveAt(0)
Display(dinosaurs)
End Sub
Private Shared Sub Display(ByVal cs As Collection(Of String))
Console.WriteLine()
For Each item As String In cs
Console.WriteLine(item)
Next item
End Sub
Private Shared Sub ChangedHandler(ByVal source As Object, _
ByVal e As DinosaursChangedEventArgs)
If e.ChangeType = ChangeType.Replaced Then
Console.WriteLine("{0} was replaced with {1}", _
e.ChangedItem, e.ReplacedWith)
ElseIf e.ChangeType = ChangeType.Cleared Then
Console.WriteLine("The dinosaur list was cleared.")
Else
Console.WriteLine("{0} was {1}.", _
e.ChangedItem, e.ChangeType)
End If
End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.Collections.ObjectModel,代码行数:134,代码来源:Collection 输出:
Psitticosaurus was Added.
Caudipteryx was Added.
Compsognathus was Added.
Muttaburrasaurus was Added.
Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus
IndexOf("Muttaburrasaurus"): 3
Contains("Caudipteryx"): True
Insert(2, "Nanotyrannus")
Nanotyrannus was Added.
dinosaurs(2): Nanotyrannus
dinosaurs(2) = "Microraptor"
Nanotyrannus was replaced with Microraptor
Remove("Microraptor")
Microraptor was Removed.
RemoveAt(0)
Psitticosaurus was Removed.
Caudipteryx
Compsognathus
Muttaburrasaurus
注:本文中的System.Collections.ObjectModel.Collection<T>类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论