Home > Net >  C# Generic variable changes if another same type generic changes
C# Generic variable changes if another same type generic changes

Time:09-08

I've this class:

public class Pair<T, V>
{
    public T A = default;
    public V B = default;

    public Pair()
    {
        A = default;
        B = default;
    }

    public Pair(T a, V b)
    {
        A = a;
        B = b;
    }

    public override bool Equals(object obj)
    {
        Pair<T, V> other = obj as Pair<T, V>;
        return A.Equals(other.A) && B.Equals(other.B);
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public override string ToString()
    {
        return "Pair: ("   A.ToString()   " , "   B.ToString()   ")";
    }
}

And I have a class with two Pair variables:

public class FakeClass<T>
{
    public T LastValue { get; protected set; } = default;
    public T CurrentValue = default;

    public void Execute() 
    {
         LastValue = CurrentValue
    }
}

public class FakeClassWithPair : FakeClass<Pair<int, int>> { }

Now if I execute this code:

FakeClassWithPair fake = new FakeClassWithPair();
fake.CurrentValue.A = 2;
fake.CurrentValue.B = 5;
fake.Execute();

fake.CurrentValue.A = 32;
fake.CurrentValue.B = 53;

In debugging Current Value and Last Value have the same value "32" and "53".

How can I avoid this?

CodePudding user response:

Classes are reference types, so when you set LastValue = CurrentValue, that means both LastValue and CurrentValue refer to the same object.

If you want Value semantics you should declare your Pair as a struct. This means that an assignment does a copy of the value. Except ofc there already are a built in type for this: ValueTuple, with some special syntax that lets you declare types like (int A, int B). There is also a regular Tuple<T1, T2> if you do want a reference type.

Also note that I see no way for your example to run, fake.CurrentValue should be initialized to null and crash when accessed. Using a value type would also solve this, since they cannot be null.

So just write change your example to FakeClassWithPair:FakeClass<(int A, int B)> and everything should work as you expect it to.

CodePudding user response:

Definitely do not roll your own class for a pair if you want value semantics. Use the built-in value tuple, defined as (T a, V b).

Also if your content of FakeClass is cloneable then you should take advantage of that (for example arrays are cloneable). So the assignment in Execute() would check if the current value implements ICloneable and proceeds accordingly.

See this example code with output. The first example with fk variable is defined by FakeClass<(int,int)> and the second example with fa variable is defined by FakeClass<int[]>. Some fun code is added to display arrays as list of vales in ToString() in order to mimic the behavior of tuples with arrays.

public class FakeClass<T>
{
    public T LastValue { get; protected set; } = default(T);
    public T CurrentValue = default(T);

    public void Execute()
    {
        if (CurrentValue is ICloneable cloneable)
        {
            LastValue = (T)cloneable.Clone();
        }
        else
        {
            LastValue = CurrentValue;
        }
    }
    public override string ToString()
    {
        if (typeof(T).IsArray)
        {
            object[] last, current;
            Array cv = CurrentValue as Array;
            if (cv != null)
            {
                current = new object[cv.Length];
                cv.CopyTo(current, 0);
            }
            else
            {
                current = new object[0];
            }
            Array lv = LastValue as Array;
            if (lv != null)
            {
                last = new object[lv.Length];
                lv.CopyTo(last, 0);
            }
            else
            {
                last = new object[0];
            }
            
            return $"Current=[{string.Join(",",current)}], Last=[{string.Join(",",last)}]";
        }
        return $"Current={CurrentValue}, Last={LastValue}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var fk = new FakeClass<(int a, int b)>();
        fk.CurrentValue = (1, 2);
        Console.WriteLine(fk);
        // Current=(1, 2), Last=(0, 0)
        fk.Execute();
        fk.CurrentValue = (3, 4);
        Console.WriteLine(fk);
        // Current=(3, 4), Last=(1, 2)

        var fa = new FakeClass<int[]>();
        fa.CurrentValue = new int[] { 1, 2 };
        Console.WriteLine(fa);
        //Current=[1,2], Last=[]
        fa.Execute();
        fa.CurrentValue = new int[] { 3, 4 };
        Console.WriteLine(fa);
        //Current=[3,4], Last=[1,2]
    }
}
  • Related