I'm learning C# currently and have a situation where I would like to have a base abstract class that other classes will inherit from.
My challenge is that I would like to pass in either an integer or a string value depending on the situation.
Currently, I can do that with a generic IF I don't constrain the generic. However, I think that might be a bad practice to not constrain a generic? And if it is a bad practice, how would I constrain the generic so that I'm only taking an integer or string.
Here's an example of what I'm trying to do:
/*
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.";
}
Thanks for your help!