作者:VB.NET开发
项目:Syste
Public Class SamplesArray
Public Shared Sub Main()
' Create and initialize a new string array.
Dim myArr As String() = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}
' Display the initial contents of the array.
Console.WriteLine("The original array initially contains:")
PrintIndexAndValues(myArr)
' Define an array segment that contains the entire array.
Dim myArrSegAll As New ArraySegment(Of String)(myArr)
' Display the contents of the ArraySegment.
Console.WriteLine("The first array segment (with all the array's elements) contains:")
PrintIndexAndValues(myArrSegAll)
' Define an array segment that contains the middle five values of the array.
Dim myArrSegMid As New ArraySegment(Of String)(myArr, 2, 5)
' Display the contents of the ArraySegment.
Console.WriteLine("The second array segment (with the middle five elements) contains:")
PrintIndexAndValues(myArrSegMid)
' Modify the fourth element of the first array segment myArrSegAll.
myArrSegAll.Array(3) = "LION"
' Display the contents of the second array segment myArrSegMid.
' Note that the value of its second element also changed.
Console.WriteLine("After the first array segment is modified, the second array segment now contains:")
PrintIndexAndValues(myArrSegMid)
End Sub
Public Shared Sub PrintIndexAndValues(arrSeg As ArraySegment(Of String))
Dim i As Integer
For i = arrSeg.Offset To (arrSeg.Offset + arrSeg.Count - 1)
Console.WriteLine(" [{0}] : {1}", i, arrSeg.Array(i))
Next i
Console.WriteLine()
End Sub
Public Shared Sub PrintIndexAndValues(myArr as String())
Dim i As Integer
For i = 0 To (myArr.Length - 1)
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub
End Class
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading.Tasks
Module Example
Private Const SegmentSize As Integer = 10
Public Sub Main()
Dim tasks As New List(Of Task)
' Create array.
Dim arr(49) As Integer
For ctr As Integer = 0 To arr.GetUpperBound(0)
arr(ctr) = ctr + 1
Next
' Handle array in segments of 10.
For ctr As Integer = 1 To CInt(Math.Ceiling(arr.Length / segmentSize))
Dim multiplier As Integer = ctr
Dim elements As Integer = If((multiplier - 1) * 10 + segmentSize > arr.Length,
arr.Length - (multiplier - 1) * 10,
segmentSize)
Dim segment As New ArraySegment(Of Integer)(arr, (ctr - 1) * 10, elements)
tasks.Add(Task.Run( Sub()
Dim list As IList(Of Integer) = CType(segment, IList(Of Integer))
For index As Integer = 0 To list.Count - 1
list(index) = list(index) * multiplier
Next
End Sub ))
Next
Try
Task.WaitAll(tasks.ToArray())
Dim elementsShown As Integer = 0
For Each value In arr
Console.Write("{0,3} ", value)
elementsShown += 1
If elementsShown Mod 18 = 0 Then Console.WriteLine()
Next
Catch e As AggregateException
Console.WriteLine("Errors occurred when working with the array:")
For Each inner As Exception In e.InnerExceptions
Console.WriteLine("{0}: {1}", inner.GetType().Name, inner.Message)
Next
End Try
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim names() As String = { "Adam", "Bruce", "Charles", "Daniel",
"Ebenezer", "Francis", "Gilbert",
"Henry", "Irving", "John", "Karl",
"Lucian", "Michael" }
Dim partNames As New ArraySegment(Of String)(names, 2, 5)
' Cast the ArraySegment object to an IList<String> and enumerate it.
Dim list = CType(partNames, IList(Of String))
For ctr As Integer = 0 To list.Count - 1
Console.WriteLine(list(ctr))
Next
End Sub
End Module