gpt4 book ai didi

c# - 类级别的结构和字符串通用约束

转载 作者:行者123 更新时间:2023-12-03 09:12:24 25 4
gpt4 key购买 nike

您好,创建一个同时具有结构和字符串作为通用约束的类有什么解决方法吗?

public class AutoEncryptor<T> where T : struct, string {
{
private T? _value;

// codes removed for brevity
}

我的目标是让自己免于创建具有相同结构的重复方法的麻烦,但区别在于,另一个接受字符串,另一个接受结构(值类型)。

最佳答案

不,这是不可能的。最好的方法是使用抽象通用基类并专门用于 stringstruct

public abstract class AutoEncryptorBase<T>
{
protected T _value;
}

public class AutoEncryptor<T> : AutoEncryptorBase<T?> where T : struct
{
}

public class TextAutoEncryptor : AutoEncryptorBase<string>
{
}

关于c# - 类级别的结构和字符串通用约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40986977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com