本文整理汇总了C#中Span.GetItem方法的典型用法代码示例。如果您正苦于以下问题:C# Span.GetItem方法的具体用法?C# Span.GetItem怎么用?C# Span.GetItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Span
的用法示例。
在下文中一共展示了Span.GetItem方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IndexOverflow
public static void IndexOverflow()
{
//
// Although Span constrains indexes to 0..2Gb, it does not similarly constrain index * sizeof(T).
// Make sure that internal offset calculcations handle the >2Gb case properly.
//
unsafe
{
byte* pMemory;
try
{
pMemory = (byte*)Marshal.AllocHGlobal((IntPtr)ThreeGiB);
}
catch (Exception)
{
return; // It's not implausible to believe that a 3gb allocation will fail - if so, skip this test to avoid unnecessary test flakiness.
}
try
{
Span<Guid> span = new Span<Guid>(pMemory, GuidThreeGiBLimit);
int bigIndex = checked(GuidTwoGiBLimit + 1);
uint byteOffset = checked((uint)bigIndex * (uint)sizeof(Guid));
Assert.True(byteOffset > (uint)int.MaxValue); // Make sure byteOffset actually overflows 2Gb, or this test is pointless.
ref Guid expected = ref Unsafe.AsRef<Guid>(((byte*)pMemory) + byteOffset);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref span.GetItem(bigIndex)));
Span<Guid> slice = span.Slice(bigIndex);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref slice.DangerousGetPinnableReference()));
slice = span.Slice(bigIndex, 1);
Assert.True(Unsafe.AreSame<Guid>(ref expected, ref slice.DangerousGetPinnableReference()));
}
finally
{
Marshal.FreeHGlobal((IntPtr)pMemory);
}
}
}