C# Generics won't allow Delegate Type Constraints

前端 未结 8 1266
别跟我提以往
别跟我提以往 2020-11-28 08:01

Is it possible to define a class in C# such that

class GenericCollection : SomeBaseCollection where T : Delegate

I couldn

8条回答
  •  -上瘾入骨i
    2020-11-28 08:35

    Yes it's possible in C# 7.3, Constraints family increased to include Enum, Delegate and unmanaged types. You can write this code without a problem:

    void M(D d, E e, T* t) where D : Delegate where E : Enum where T : unmanaged
        {
    
        }
    

    From Docs:

    Beginning with C# 7.3, you can use the unmanaged constraint to specify that the type parameter must be a non-nullable unmanaged type. The unmanaged constraint enables you to write reusable routines to work with types that can be manipulated as blocks of memory

    Useful links:

    The future of C#, from Microsoft Build 2018

    What's new in C# 7.3?

提交回复
热议问题