Now I know that C# doesn't support inheritance of structs for a number of reasons. I was wondering however, if it is still possible to implement some form of hierarchy for data types such that a function can rely on a variable of type B or type C having field x because they both inherit from type A.
More specific: I have two structs that have a value field and coordinates (2D and 3D)
public struct BarDataPoint
{
public TwoTuple<float> coords { get; }
public float value { get; }
public BarDataPoint(TwoTuple<float> coords, float value)
{
this.coords = coords;
this.value = value;
}
}
public struct ScatterDataPoint
{
public ThreeTuple<float> coords { get; }
public float value { get; }
public ScatterDataPoint(ThreeTuple<float> coords, float value)
{
this.coords = coords;
this.value = value;
}
}
I also have this function that is supposed to find the maximum value of an array of data points
{
// Finds the maximum value of an BarData array
float maxValue = data[0].value;
foreach (BarDataPoint dataPoint in data)
{
maxValue = (maxValue < dataPoint.value) ? dataPoint.value : maxValue;
}
return maxValue;
}
I would like to generalize this function, so that it can take both types (or any other type with a "value" field) and return its maximum value.
Is this possible? If yes, how? Or would you recommend a completely different design?