本文整理汇总了C#中System.ArraySegment<T>结构体的典型用法代码示例。如果您正苦于以下问题:C# ArraySegment<T>结构体的具体用法?C# ArraySegment<T>怎么用?C# ArraySegment<T>使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
ArraySegment<T>结构体属于System命名空间,在下文中一共展示了ArraySegment<T>结构体的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class SamplesArray {
public static void Main() {
// Create and initialize a new string array.
String[] myArr = { "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.
ArraySegment<String> myArrSegAll = new ArraySegment<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.
ArraySegment<String> myArrSegMid = new ArraySegment<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 );
}
public static void PrintIndexAndValues( ArraySegment<String> arrSeg ) {
for ( int i = arrSeg.Offset; i < (arrSeg.Offset + arrSeg.Count); i++ ) {
Console.WriteLine( " [{0}] : {1}", i, arrSeg.Array[i] );
}
Console.WriteLine();
}
public static void PrintIndexAndValues( String[] myArr ) {
for ( int i = 0; i < myArr.Length; i++ ) {
Console.WriteLine( " [{0}] : {1}", i, myArr[i] );
}
Console.WriteLine();
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:51,代码来源:ArraySegment 输出:
The original array initially contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
The first array segment (with all the array's elements) contains:
[0] : The
[1] : quick
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
The second array segment (with the middle five elements) contains:
[2] : brown
[3] : fox
[4] : jumps
[5] : over
[6] : the
After the first array segment is modified, the second array segment now contains:
[2] : brown
[3] : LION
[4] : jumps
[5] : over
[6] : the
示例2: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class Example
{
private const int segmentSize = 10;
public static async Task Main()
{
List<Task> tasks = new List<Task>();
// Create array.
int[] arr = new int[50];
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++)
arr[ctr] = ctr + 1;
// Handle array in segments of 10.
for (int ctr = 1; ctr <= Math.Ceiling(((double)arr.Length)/segmentSize); ctr++) {
int multiplier = ctr;
int elements = (multiplier - 1) * 10 + segmentSize > arr.Length ?
arr.Length - (multiplier - 1) * 10 : segmentSize;
ArraySegment<int> segment = new ArraySegment<int>(arr, (ctr - 1) * 10, elements);
tasks.Add(Task.Run( () => { IList<int> list = (IList<int>) segment;
for (int index = 0; index < list.Count; index++)
list[index] = list[index] * multiplier;
} ));
}
try {
await Task.WhenAll(tasks.ToArray());
int elementsShown = 0;
foreach (var value in arr) {
Console.Write("{0,3} ", value);
elementsShown++;
if (elementsShown % 18 == 0)
Console.WriteLine();
}
}
catch (AggregateException e) {
Console.WriteLine("Errors occurred when working with the array:");
foreach (var inner in e.InnerExceptions)
Console.WriteLine("{0}: {1}", inner.GetType().Name, inner.Message);
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:46,代码来源:ArraySegment 输出:
1 2 3 4 5 6 7 8 9 10 22 24 26 28 30 32 34 36
38 40 63 66 69 72 75 78 81 84 87 90 124 128 132 136 140 144
148 152 156 160 205 210 215 220 225 230 235 240 245 250
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
String[] names = { "Adam", "Bruce", "Charles", "Daniel",
"Ebenezer", "Francis", "Gilbert",
"Henry", "Irving", "John", "Karl",
"Lucian", "Michael" };
var partNames = new ArraySegment<String>(names, 2, 5);
// Cast the ArraySegment object to an IList<String> and enumerate it.
var list = (IList<String>) partNames;
for (int ctr = 0; ctr <= list.Count - 1; ctr++)
Console.WriteLine(list[ctr]);
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:ArraySegment 输出:
Charles
Daniel
Ebenezer
Francis
Gilbert
注:本文中的System.ArraySegment<T>结构体示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论