相关疑难解决方法(0)

T [].包含struct和class的行为方式不同

这是一个后续问题: List <T> .Contains和T [].包含的行为不同

T[].ContainsT类和结构时表现不同.假设我有这个结构:

public struct Animal : IEquatable<Animal>
{
    public string Name { get; set; }

    public bool Equals(Animal other) //<- he is the man
    {
        return Name == other.Name;
    }
    public override bool Equals(object obj)
    {
        return Equals((Animal)obj);
    }
    public override int GetHashCode()
    {
        return Name == null ? 0 : Name.GetHashCode();
    }
}

var animals = new[] { new Animal { Name = "Fred" } };

animals.Contains(new Animal { Name = …
Run Code Online (Sandbox Code Playgroud)

c# arrays generics struct equals

12
推荐指数
1
解决办法
309
查看次数

System.Array是否对值类型执行装箱?

我最近List<>[]一系列小型结构的vs 做了一些粗略的性能测量.System.Array似乎赢得了胜利,所以我顺其自然.

只是在我看来System.Array包含对象类型,所以肯定用结构填充它会导致拳击发生?

但是,System.Array的MSDN条目指出:

在.NET Framework 2.0版,Array类实现 System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>以及 System.Collections.Generic.IEnumerable<T>通用接口.这些实现在运行时提供给数组,因此文档构建工具不可见.因此,通用接口不会出现在Array类的声明语法中,并且没有可通过将数组转换为通用接口类型(显式接口实现)来访问的接口成员的参考主题.

这是否意味着毕竟不会发生拳击?(并会解释我的表现结果)

c# arrays boxing

11
推荐指数
1
解决办法
2497
查看次数

标签 统计

arrays ×2

c# ×2

boxing ×1

equals ×1

generics ×1

struct ×1