c# - 无法在 C# 的通用约束中使用结构类型

标签 c# generics constraints

当我发现问题时,我正在玩通用约束。除了它在编译时显示的错误提示外,我无法找到它的答案。

struct MyStruct
{
    public void Get()
    {

    }
}

class MyClass
{
    public void Get()
    {

    }
}


public class Stable<T> where T : MyStruct // NOT Allowed
{

}

public class Stable<T> where T : MyClass //Allowed
{

}

public class Stable<T> where T : struct //Allowed
{

}

public class Stable<T> where T : class //Allowed
{

}

最佳答案

结构默认是密封的,所以你不应该在这里使用泛型。

使用:

public class StableStruct : Stable<MyStruct>
{
}

关于c# - 无法在 C# 的通用约束中使用结构类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50130944/

相关文章:

c# - 使用 stardog : "cannot execute update query on read endpoint" 从 C# 发送 SPARQL 查询

c# - IO 异常 : directory name is invalid using directory from File System Watcher OnChanged Event

java - 将通用数组列表添加到数组列表

c# - 在类属性中使用泛型类型的名称

java - 通过 Jackson 反序列化通用对象时出错

ios - 解决 Storyboard 缺少尾随/前导约束警告

ios - UIPageViewController 未调整其 subview Controller 的大小

c# - 在 ASP.NET 中使用 WordPress RSS 提要

swift3 - 如何以编程方式为一行添加自动布局约束

c# - 如何使用 EPPLUS 从 Excel 电子表格单元格中获取值?