Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建具有泛型的抽象基类,该泛型可以处理整型值或字符串值?_C#_Generics_Inheritance - Fatal编程技术网

C# 如何创建具有泛型的抽象基类,该泛型可以处理整型值或字符串值?

C# 如何创建具有泛型的抽象基类,该泛型可以处理整型值或字符串值?,c#,generics,inheritance,C#,Generics,Inheritance,我目前正在学习C#,我希望有一个基础抽象类,其他类将从中继承 我的挑战是,我想根据情况传入一个整数或字符串值 目前,如果不约束泛型,我可以使用泛型来实现这一点。然而,我认为不限制泛型是一种不好的做法?如果这是一个糟糕的做法,我将如何约束泛型,使我只接受整数或字符串 下面是我尝试做的一个例子: /* I want to have a base abstract class that can handle both an integer value and a string value */ pub

我目前正在学习C#,我希望有一个基础抽象类,其他类将从中继承

我的挑战是,我想根据情况传入一个整数或字符串值

目前,如果不约束泛型,我可以使用泛型来实现这一点。然而,我认为不限制泛型是一种不好的做法?如果这是一个糟糕的做法,我将如何约束泛型,使我只接受整数或字符串

下面是我尝试做的一个例子:

/*
I want to have a base abstract class that can handle
both an integer value and a string value
*/
public abstract class Characteristic<T> where T : int, string {
    private T _value;

    public T Value {
        get { return _value;}
        set { _value = value;}
    }
}

public class NumericAttribute : Characteristic<int> {
    private int _modifiedValue = Value + 1;
}

public class StringAttribute : Characteristic<string> {
    private string _modifiedValue = Value + " more text.";
}
/*
我想要一个能处理
整数值和字符串值
*/
公共抽象类特征,其中T:int,string{
私人T_值;
公共价值{
获取{返回_值;}
设置{u value=value;}
}
}
公共类数字属性:特征{
private int_modifiedValue=Value+1;
}
公共类StringAttribute:特性{
私有字符串_modifiedValue=Value+“更多文本。”;
}

谢谢你的帮助

将泛型类限制为仅
int
string
是不可能的

  • 没有“仅这些类”约束
  • int
    string
    不共享它们所独有的任何公共基类或接口
  • int
    是一个
    struct
    string
    是一个
    ——因此,即使通过
    struct
    /
    约束来缩小选择范围也是不可能的
因此,基本上没有类型约束的泛型(如果确实需要,可以在运行时检查类型)是使用这两种类型可以获得的最佳泛型类型。如果泛型不约束它们键入的参数(即
列表
),则没有什么特别的错误

如果您想将类型至少缩小一点
int
string
确实有一些共享接口-
IComparable
IConvertible
(非泛型一次),但这些接口是由所有数字类型实现的

注意:有一些类似的问题试图将泛型限制为“数字类型”,这可能会提供一些替代方法(包括代码生成)。也就是说,

或多或少地按照你的要求去做是可能的;但正如其他人已经指出的,您可能不想这样做(甚至可能不需要约束)。然而,正如我在评论中指出的,您创建了一个接口

public interface IModifiable<T>
{
    T Modify(T value);
}
public abstract class Characteristic<T> where T : IModifiable<T>
{
    private T _value;

    public T Value
    {
        get { return _value; }
        set { _value = value; }
    }
}
基类现在对接口有一个约束

public interface IModifiable<T>
{
    T Modify(T value);
}
public abstract class Characteristic<T> where T : IModifiable<T>
{
    private T _value;

    public T Value
    {
        get { return _value; }
        set { _value = value; }
    }
}
公共抽象类特征,其中T:IModifiable
{
私人T_值;
公共价值
{
获取{返回_值;}
设置{u value=value;}
}
}
和你的派生类,和以前一样

public class NumericAttribute : Characteristic<Int32>
{
    void f()
    {
        var _modifiedValue = Value.Modify(new Int32() { Value = 1 });
    }
}

public class StringAttribute : Characteristic<String>
{
    void f()
    {
        var _modifiedValue = Value.Modify(new String() { Value = " more text." });
    }
}
公共类数字属性:特征
{
void f()
{
var_modifiedValue=Value.Modify(新的Int32(){Value=1});
}
}
公共类StringAttribute:特性
{
void f()
{
var_modifiedValue=Value.Modify(新字符串(){Value=“more text.”);
}
}

再次,虽然这给了你一个具体问题的“解决方案”,你可能会考虑这种方法的智慧。

你为什么需要约束?
Characteristic
基类从不操纵
Value
,子类知道它们正在处理的具体类型,因此可以执行该操作;这在C#中根本不存在。下面是如何创建的示例: