This is supposed to be an immutable structure representing a time-stamped value. Since it represents a physical measurement, it cannot change, and I want to make sure it really doesn't change accidentally at runtime.
public struct Sample
{
public TimeSpan Time { get { return _time; } }
private readonly TimeSpan _time;
public Double Value { get { return _value; } }
private readonly Double _value;
public Sample(TimeSpan time, Double value) : this()
{
_time = time;
_value = value;
}
}
I have heard a lot about "properties are better than fields", and it's common to see the following idiom:
public Double Foo { get; private set; }
But I wonder:
- Does it make a difference? I feel I prefer the current, field-based form, but maybe I am missing something...
- In case it does, which one is preferrable, regarding immutability (and also other characteristics)?