Perform add operation using Generic Concept
I have a class of generic type which accepts 2 parameters . now I need to add these two parameter values and return back the result.
class AddValue<TText, TValue>
{
public AddValue(TText text, TValue value)
{
Text = text;
Value = value;
开发者_JAVA百科 }
public TText Text { get; set; }
public TValue Value { get; set; }
}
If you literally mean Add, as in TText + TValue
then you have two options here:
You can force them to implement an interface that has an Add method (or whatever), and call that, or
Have the caller pass a delegate that does the addition.
Since unbounded generic type-parameters are effectively object
references, you can't operate on them directly. Instead, get the calling code to do the heavy lifting, since they know what to do.
精彩评论