4

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:

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 multiplication UncNumber[,] 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?

Wollmich
  • 1,616
  • 1
  • 18
  • 46
  • 4
    `A lot of the instances of the UncNumber struct will be very short-lived and immutable.` The type is most certainly not immutable. You have public fields. They can be mutated. – Servy Oct 06 '17 at 15:11
  • @Servy I could have private fields, readonly properties and a constructor `UncNumber(double value, DependsOn[] dependencies)`. So it should be immutable. – Wollmich Oct 06 '17 at 15:14
  • 1
    Yes, you could make it immutable by doing that. Note that `DependsOn` is also currently mutable, when it really shouldn't be. Also note that if you return the array from `UncNumber`, even if you don't let the array *variable* be mutated, the returned array is mutable and could be mutated. To make the type properly immutable you'd need to return a read-only collection of some type (i.e. `IEnumerable` or `IReadOnlyList`, depending on what you need). – Servy Oct 06 '17 at 15:17
  • @Sery I'm aware about the immutable stuff. I just wrote the code in post like that for simplification. Should I edit my code in the question? – Wollmich Oct 06 '17 at 15:19
  • If your actual structs are immutable, then yes, you should edit the question to reflect that, as it's a highly relevant difference. – Servy Oct 06 '17 at 15:21
  • @Sery I've edited the question. I'm not using `public ReadOnlyCollection Dependencies { get { return Array.AsReadOnly(_dependencies) ; } }` because the read only wrapper for the array will slow done stuff, because an additional instance for the wrapper has to be created. – Wollmich Oct 06 '17 at 15:45
  • I mentioned two options for a read only operation that don't require a new instance. But either way, if you aware that the type is mutable in that way, that's your decision. As the struct itself isn't mutable it avoids *most* of the problems, but it is still somewhat unexpected for a struct to expose a reference to a mutable object. – Servy Oct 06 '17 at 15:49
  • @Sery Either if I'm implementing `IEnumerable` or I'm using `IReadOnlyList` wrapper there will be an array behind to store the data. So there a two instance of reference types. One for the array the other for the class implementing `IEnumerable` or the wrapper class `IReadOnlyList`. On the other hand If I'm just using the array there is just one instance of a reference type, the array itself. That's the reason why it's faster. I'm aware that the array is mutable but I'm just using it readonly. – Wollmich Oct 06 '17 at 15:58
  • You don't need a wrapper. Arrays implement both of those interfaces. – Servy Oct 06 '17 at 16:00

0 Answers0