简体   繁体   English

不同泛型类型中的非泛型成员是否相同

[英]Doest non-generic members in different generic types are same or not

public sealed class Reference<T>
{
    private static ulong maxID = 0;
    private ulong id = maxID++;
    public ulong LongID => this.id;
    public Reference() { }
    public bool Equals(T? other)
        => other?.Reference.id == this.id;
    public bool Equals(Reference<T>? other)
        => other?.id == this.id;
    public override bool Equals(object? obj)
        => obj is T it ? Equals(it) : obj is Reference<T> rf ? Equals(rf) : false;
    public override int GetHashCode() => HashCode.Combine(this.id);

    public static bool operator ==(Reference<T>? left, Reference<T>? right)
        => left?.Equals(right) ?? false;

    public static bool operator !=(Reference<T>? left, Reference<T>? right)
        => !(left == right);

    public override string ToString() => $"Reference<{typeof(T).Name}> ({this.id})";
}

In this code i have class Reference<T> with static field.在此代码中,我有 class Reference<T>和 static 字段。 if i create new instance, hes got individual id如果我创建新实例,他有个人 ID
new Reference<T>(); id equals 0 id 等于 0
new Reference<T>(); id equals 1 ID 等于 1
In do.net when you use generic type under hood created new non-generic type在 do.net 中,当您在引擎盖下使用泛型类型时创建了新的非泛型类型
I think that Reference<T>.maxID has value 2 and Reference<T2>.maxID 0我认为Reference<T>.maxID的值为 2 而Reference<T2>.maxID为 0
But this fields non-generic, and it is quite possible that fields are sames in different generic types但是这个字段是非通用的,很可能字段在不同的通用类型中是相同的

From C# 6 draft specification's section 8.4.3 Open and closed types :来自 C# 6 规范草案第8.4.3 节开放和封闭类型

Each closed constructed type has its own set of static variables, which are not shared with any other closed constructed types.每个封闭构造类型都有自己的一组 static 个变量,这些变量不与任何其他封闭构造类型共享。 Since an open type does not exist at run-time, there are no static variables associated with an open type.由于运行时不存在开放类型,因此没有 static 个变量与开放类型关联。 Two closed constructed types are the same type if they are constructed from the same unbound generic type, and their corresponding type arguments are the same type.如果两个封闭构造类型是从相同的未绑定泛型类型构造的,则它们是相同类型,并且它们对应的类型 arguments 是相同类型。

So for every different T in Reference<T>.maxID there will be different maxID .因此,对于Reference<T>.maxID中的每个不同的T ,都会有不同maxID

In do.net when you use generic type under hood created new non-generic type在 do.net 中,当您在引擎盖下使用泛型类型时创建了新的非泛型类型

Note that there is a small caveat here.请注意,这里有一个小警告。 Depending on the generic type parameters used (in particular if they are reference or value types ) CLR implementation of C# can behave differently in terms of method compilation.根据所使用的泛型类型参数(特别是如果它们是引用或值类型),C# 的 CLR 实现在方法编译方面可能表现不同。 To prevent generic code bloat for reference types methods code is reused while for every value type new implementation will be compiled.为了防止引用类型方法的通用代码膨胀,代码被重用,而对于每个值类型,将编译新的实现。 See .NET Generics and Code Bloat article.请参阅.NET Generics 和 Code Bloat文章。

Yes, when you use generic type with new generic parameter(s) under the hood creates new non-generic type and all non-generic members also copy是的,当您使用带有新泛型参数的泛型类型时,在后台创建新的非泛型类型并且所有非泛型成员也会复制

new Reference<Int32>(); id are 0编号为 0
new Reference<UInt32>(); id are 0, cause its different type id 为 0,导致其类型不同

If i want to different result, i should create new non-generic class (not inside Reference<T>) with public|internal static field maxID and using him如果我想要不同的结果,我应该使用 public|internal static 字段maxID创建新的非通用 class(不在 Reference<T> 内)并使用他

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM