本文整理汇总了C#中Span.CreateArray方法的典型用法代码示例。如果您正苦于以下问题:C# Span.CreateArray方法的具体用法?C# Span.CreateArray怎么用?C# Span.CreateArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Span
的用法示例。
在下文中一共展示了Span.CreateArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks
public void CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array)
{
Span<byte> span = new Span<byte>(array);
Assert.Equal(array.Length, span.Length);
Assert.NotSame(array, span.CreateArray());
Assert.False(span.Equals(array));
Span<byte>.Enumerator it = span.GetEnumerator();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
array[i] = unchecked((byte)(array[i] + 1));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
span.Slice(i).Write<byte>(unchecked((byte)(array[i] + 1)));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
span.Slice(i).Write<MyByte>(unchecked(new MyByte((byte)(array[i] + 1))));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
}
Assert.False(it.MoveNext());
it.Reset();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
}
Assert.False(it.MoveNext());
}
示例2: CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks
public void CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array)
{
Span<byte> span = new Span<byte>(array);
Assert.Equal(array.Length, span.Length);
Assert.NotSame(array, span.CreateArray());
Assert.True(span.ReferenceEquals(span));
Assert.True(span.Equals(span));
Assert.True(span.Equals((object)span));
Assert.Equal(span.GetHashCode(), span.GetHashCode());
Assert.False(span.Equals(array));
Span<byte>.Enumerator it = span.GetEnumerator();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
array[i] = unchecked((byte)(array[i] + 1));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
span.Slice(i).Write<byte>(unchecked((byte)(array[i] + 1)));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
span.Slice(i).Write<MyByte>(unchecked(new MyByte((byte)(array[i] + 1))));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
}
Assert.False(it.MoveNext());
it.Reset();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
}
Assert.False(it.MoveNext());
{
Span<byte> sameSpan = new Span<byte>(array);
Assert.Equal(span.GetHashCode(), sameSpan.GetHashCode());
Assert.True(span.ReferenceEquals(sameSpan));
Assert.True(span.Equals(sameSpan));
Assert.True(span.Equals((object)sameSpan));
}
{
Span<byte> structCopy = span;
Assert.Equal(span.GetHashCode(), structCopy.GetHashCode());
Assert.True(span.ReferenceEquals(structCopy));
Assert.True(span.Equals(structCopy));
Assert.True(span.Equals((object)structCopy));
}
{
byte[] differentArray = new byte[array.Length * 2];
for (int i = 0; i < array.Length; i++)
{
differentArray[i] = unchecked((byte)(array[i] + 1));
differentArray[array.Length + i] = array[i];
}
{
Span<byte> equivalentSpan = new Span<byte>(differentArray, array.Length, array.Length);
Assert.Equal(span.GetHashCode(), equivalentSpan.GetHashCode());
Assert.False(span.ReferenceEquals(equivalentSpan));
Assert.True(span.Equals(equivalentSpan));
Assert.True(span.Equals((object)equivalentSpan));
if (equivalentSpan.Length > 0)
{
Span<byte> similarSpan = equivalentSpan.Slice(0, equivalentSpan.Length - 1);
Assert.False(span.ReferenceEquals(similarSpan));
Assert.False(span.Equals(similarSpan));
Assert.False(span.Equals((object)similarSpan));
}
}
{
Span<byte> differentSpan = new Span<byte>(differentArray, 0, array.Length);
Assert.False(span.ReferenceEquals(differentSpan));
// This can be simplified although it is harder to understand after simplification
if (array.Length == 0)
{
Assert.True(span.Equals(differentSpan));
Assert.True(span.Equals((object)differentSpan));
}
else
{
Assert.False(span.Equals(differentSpan));
Assert.False(span.Equals((object)differentSpan));
}
}
}
//.........这里部分代码省略.........