how to update class that changes frequently? reinstatiate every time or just update fields?
<p>I need to update <code>InstrumentInfo</code> class frequently. I update this class from one thread and access (read) from another.</p>

<p>I have <code>Instrument</code> class. For each <code>Instrument</code> class I need to maintain <code>InstrumentInfo</code>:</p>

<pre><code>// omit class Instrument as not improtant

public class InstrumentInfo
{
    public string Name { get; set; }
    public TradingStatus Status { get; set; }
    public decimal MinStep;
    public double ValToday;
    public decimal BestBuy;
    public decimal BestSell;
}

public class DerivativeInfo : InstrumentInfo
{
    public DateTime LastTradeDate { get; set; }
    public DateTime ExpirationDate { get; set; }
    public string UnderlyingTicker { get; set; }
}

// i do have several more subclasses
</code></pre>

<p>I do have two options:</p>

<ol>
<li>Create only one <code>InstrumentInfo</code> for each <code>Instrument</code>. When some field updates, for example <code>BestBuy</code> just update value of this field. Clients should obtain <code>InstrumentInfo</code> only once and use it during entire application lifetime.</li>
<li>On each update create new instance of <code>InstrumentInfo</code>. Clients should obtain every time the most recent copy of InstrumentInfo.</li>
</ol>

<p>With <code>1</code> I do need to lock, because <code>decimal</code> <code>DateTime</code> <code>string</code> update is not guaranteed to be atomic. But I don't need to reinstatiate object.</p>

<p>With <code>2</code> I don't need to lock at all, as <code>reference</code> update is atomic. But I likely will use more memory and I will probably create more work for GC because every time I need to instatiate new object (and initialize all fields).</p>

<p><code>1</code> implementation</p>

<pre><code>    private InstrumentInfo[] instrumentInfos = new InstrumentInfo[Constants.MAX_INSTRUMENTS_NUMBER_IN_SYSTEM];

    // invoked from different threads
    public InstrumentInfo GetInstrumentInfo(Instrument instrument)
    {
        lock (instrumentInfos) {
            var result = instrumentInfos[instrument.Id];
            if (result == null) {
                result = new InstrumentInfo();
                instrumentInfos[instrument.Id] = result;
            }
            return result;
        }
    }

    ...........
    InstrumentInfo ii = GetInstrumentInfo(instrument);
    lock (ii) {
        ii.BestSell = BestSell;
    }
</code></pre>

<p><code>2</code> implementation:</p>

<pre><code>    private InstrumentInfo[] instrumentInfos = new InstrumentInfo[Constants.MAX_INSTRUMENTS_NUMBER_IN_SYSTEM];

    // get and set are invoked from different threads
    // but i don't need to lock at all!!! as reference update is atomic
    public void SetInstrumentInfo(Instrument instrument, InstrumentInfo info)
    {
        if (instrument == null || info == null)
        {
            return;
        }
        instrumentInfos[instrument.Id] = info;
    }

    // get and set are invoked from different threads
    public InstrumentInfo GetInstrumentInfo(Instrument instrument)
    {
        return instrumentInfos[instrument.Id];
    }
    ....
    InstrumentInfo ii = new InstrumentInfo {
        Name = ..
        TradingStatus = ...
        ...
        BestSell = 
    }

    SetInstrumentInfo(instrument, ii); // replace InstrumentInfo
</code></pre>

<p>So what do you think? I want to use approach <code>2</code> because I like code without locks! Am I correct that I do not need <code>lock</code> at all as I just replace reference? Do you aggree that <code>2</code> is preferred? Any suggestions are welcome.</p>
