Generic parameter class with HasChanged functionality I have designed a class that allows the client to set an initial value. The idea is that if later on we wish to update this value then we can use the Update method and pass in the new value. We can then use the HasChanged() method to do something with the new value if it is different than the initial value. 

public class UpdatableParam&lt;T&gt;
{
    private readonly T m_initialValue;
    private T m_updatedValue;

    public T InitialValue
    {
        get { return m_initialValue; }
    }

    public T UpdatedValue
    {
        get { return m_updatedValue; }
    }

    public UpdatableParam(T initialValue)
    {
        m_initialValue = ObjectCopier.Clone(initialValue);
        m_updatedValue = ObjectCopier.Clone(initialValue);
    }

    public UpdatableParam(T initialValue, T updatedValue)
    {
        m_initialValue = ObjectCopier.Clone(initialValue);
        m_updatedValue = ObjectCopier.Clone(updatedValue);
    }

    public void Update(T updatedValue)
    {
        m_updatedValue = ObjectCopier.Clone(updatedValue);
    }

    public virtual bool HasChanged()
    {
        return !EqualityComparer&lt;T&gt;.Default.Equals(m_initialValue, m_updatedValue);
    }
}


To ensure that the values cannot be changed from outside of the class after being added I have deep copied from the values passed in, both from the constructor and the update method. 

The HasChanged() method works for most types I care about and I have one overloaded version of the class for lists. The implementation is as follows:

public class UpdatableListParam&lt;T&gt; : UpdatableParam&lt;List&lt;T&gt;&gt;
{
    public UpdatableListParam(List&lt;T&gt; initialValue)
        : base(initialValue)
    {

    }

    public override bool HasChanged()
    {
        var initialValue = InitialValue;
        var updatedValue = UpdatedValue;

        if (initialValue == null &amp;&amp; updatedValue == null) return false;
        if (initialValue == null || updatedValue == null) return true;

        return !initialValue.SequenceEqual(updatedValue);
    }
}


I understand I could use IEnumerable rather than lists but have not yet implemented that change. 

Is there anything fundamentally wrong with the above code? Is there any design change I could make to improve functionality? Or better yet has someone already beat me to this and is there anything already available perhaps in a NuGet package that does the above for me? 

Edit - Adding Use Case

class SomeSourceInfo
{
    private SomeSource m_someSource;
    private UpdatableParam&lt;string&gt; m_name;

    public SomeSourceInfo(ISomeSource someSource)
    {
         m_someSource = someSource;
    }

    public void PopulateFromSource()
    {
        // Query from a source(could be a db etc.)
        m_someSource.Refresh();
        var srcName = m_someSource.Name;
        m_name = new UpdatableParam&lt;string&gt;(srcName);
    }

    public void UploadToSource()
    {
        if(m_name.HasChanged())
        {
             // Upload back to source
             m_someSource.Name = m_name.UpdatedValue;
        }

        PopulateFromSource();
    }

    public void UpdateSourceName(string newName)
    {
        // Allow changing of the name through UI calls or API etc.
        m_name.Update(newName);
    }
}


This only shows one potential param but in real life we may have hundreds of real params we can update and we only want them to upload back to the source when the user tells us to through the  UploadToSource() method and therefore need to cache the values in the mean time.
I'm still not sure about its usage but here's the review.




public T InitialValue
{
    get { return m_initialValue; }
}

public T UpdatedValue
{
    get { return m_updatedValue; }
}



As a user of this class I most probably don't want to think about which value I should use. I bet it'll be always the latest one so a single property like Value returning the most up-to-date value would be more appropriate.




public UpdatableParam(T initialValue, T updatedValue)
{
    m_initialValue = ObjectCopier.Clone(initialValue);
    m_updatedValue = ObjectCopier.Clone(updatedValue);
}



Initializing the class already with an updated-value is somehow counterintuitive to me. Why would I want to do that? I thought it should track changes and not be created in a changed state already.




public UpdatableParam(T initialValue)
{
    m_initialValue = ObjectCopier.Clone(initialValue);
    m_updatedValue = ObjectCopier.Clone(initialValue);
}



I don't want to update the value. I just want to set a new one via the Value property. The object tracking it should be able to figure out what's going on which leads me to the next point:


public virtual bool HasChanged()
{
    return !EqualityComparer&lt;T&gt;.Default.Equals(m_initialValue, m_updatedValue);
}



This should a property, usulay like IsDirty.



With all suggestions applied I could imagine the new clean object could be implemented like this:

public class TrackingValue&lt;T&gt;
{
    private readonly T _initialValue;
    private T _newValue;

    public TrackingValue(T initialValue)
    {
        _initialValue = ObjectCopier.Clone(initialValue);
        _newValue = _initialValue; // No cloning, it's the same value.
    }

    public T Value
    {
        get =&gt; IsDirty ? _newValue : _initialValue;
        set =&gt; _newValue = ObjectCopier.Clone(value);
    }

    public virtual bool IsDirty =&gt; !EqualityComparer&lt;T&gt;.Default.Equals(_initialValue, _newValue);

    public static implicit operator T(TrackingValue&lt;T&gt; value) =&gt; value.Value;
}


Additionally I want to be able to use the value without effort thus the implicit cast operator so that I can put the wrapper where I usually use the actual value and the compile will figure out the rest.