Arrays, inheritance, generics I had a requirement where I need to create object using same interface but one derived class expects simple data and other array of type. Here is solution I came up with:

I had two questions:


As you can see I am converting IEquatable&lt;T&gt; into T. In my testing so far I did not see any disasters. Can you think of any from top off your head?
Is there a way I can change constraint in ArrayExpectant::CloneFromMe to ArrayWrapper instead of IEquatable?




public class ArrayWrapper&lt;T&gt; : IEquatable&lt;T&gt;
{
    public T[] Array { get; set; }

    public bool Equals(T other) { return false;  }
    public bool Equals(T[] other) { return Array == other; }
    public bool Equals(ArrayWrapper&lt;T&gt; other) { return Array == other.Array; }
}

public interface ICreationByCloning
{
    ICreated CloneFromMe&lt;TClone&gt;(IEquatable&lt;TClone&gt; t) where TClone : IEquatable&lt;TClone&gt;;
}

public interface ICreated { }

public class Simpleton&lt;T&gt; : ICreated, ICreationByCloning  
{
    public Simpleton(T t) { }
    public ICreated CloneFromMe&lt;TClone&gt;(IEquatable&lt;TClone&gt; t) where TClone : IEquatable&lt;TClone&gt;
    {
        return new Simpleton&lt;TClone&gt;((TClone)t); 
    }  
}

public class ArrayExpectant&lt;T&gt; : ICreated, ICreationByCloning where T : IEquatable&lt;T&gt;
{

    T[] Data { get; set; }
    public ArrayExpectant(T[] t) { Data = t; }
    public ICreated CloneFromMe&lt;TClone&gt;(IEquatable&lt;TClone&gt; t) where TClone : IEquatable&lt;TClone&gt;
    {
        TClone[] array = (t as ArrayWrapper&lt;TClone&gt;).Array;

        return new ArrayExpectant&lt;TClone&gt;(array); 
    }
}
I'm not completely sure what are you trying to accomplish, but I think this is bad and very confusing code.

You seem to be using the interface IEquatable&lt;T&gt; to mean “T, or a type that has some relation to T”. That's not what it's meant for at all!

Also, the point of interfaces is to be able to use different types in the same way. For example, if I have a IEnumerable&lt;string&gt;, I know I can iterate over it using a foreach and process the strings it gives me.

But that does not apply to your interface. I have to know whether I have have Simpleton&lt;T&gt; (I have to pass T to it) or ArrayExpectant&lt;T&gt; (I have to pass ArrayWrapper&lt;T&gt; to it).

Because of this, I would suggest you to drop the interface completely, I don't see any benefit of having it.