当前位置: 首页>>代码示例>>C#>>正文


C# Span.GetHashCode方法代码示例

本文整理汇总了C#中Span.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# Span.GetHashCode方法的具体用法?C# Span.GetHashCode怎么用?C# Span.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Span的用法示例。


在下文中一共展示了Span.GetHashCode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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.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));
                    }
                }
            }
//.........这里部分代码省略.........
开发者ID:Richard-FF,项目名称:corefxlab,代码行数:101,代码来源:UsageScenarioTests.cs


注:本文中的Span.GetHashCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。