I've the following struct with an array as second field:
public struct UncNumber
{
private double _value;
private DependsOn[] _dependencies;
public double Value { get { return _value; } }
public DependsOn[] Dependencies { get { return _dependencies; } }
public UncNumber(double value, DependsOn[] dependencies)
{
_value = value;
_dependencies = dependencies;
}
}
By the way the DependsOn
struct looks like that:
public struct DependsOn
{
private int _input;
private double _jacobi;
public int Input { get { return _input; } }
public double Jacobi { get { return _jacobi; } }
public DependsOn(int input, double jacobi)
{
_input = input;
_jacobi = jacobi;
}
}
I looked at the following resources:
- What's the difference between struct and class in .NET?
- When should I use a struct instead of a class?
- Does it make sense to define a struct with a reference type member?
But I didn't came to a conclusion yet if I should use a struct or a class for UncNumber
. I've the following additional comments why I think a struct is the better choice in this case:
- I'm using the above
UncNumber
structure for mathematical computation, similiar like I could use the Complex Structure. - I'm having a lot of methods which use arrays of the
UncNumber
struct. E.g.: matrix multiplicationUncNumber[,] Dot(UncNumber[,] a, UncNumber[,] b)
. - A lot of the instances of the
UncNumber
struct will be very short-lived and immutable. - The instance size of the
UncNumber
struct is 16 bytes (8 bytes for the double Value and 8 bytes for the reference type / array Dependencies).
Or does a struct with an array field not make sense at all?