Wrapper that accepts both scalar and collection Can I somehow refactor this into one class? Some things are common here like Metadata property.

public class Resource&lt;TEntity&gt;
{
    public TEntity Data { get; set; }

    public Dictionary&lt;string, string&gt; Metadata { get; set; }

    public Resource(TEntity data)
    {
        Data = data;
    }
}

public class ResourceCollection&lt;TEntity&gt;
{
    public ICollection&lt;TEntity&gt; Data { get; set; }

    public Dictionary&lt;string, string&gt; Metadata { get; set; }

    public int Count { get; set; }

    public ResourceCollection(ICollection&lt;TEntity&gt; data)
    {
        Data = data;

        Count = data.Count;
    }
}


Is this possible to make interface that expects either ICollection&lt;TEntity&gt; or TEntity alone? 

This wrapper class is used for serialization it simply acts as a wrapper, but still I have some doubts about it that it could be done better 

I also came up with this:

public class Resource&lt;T&gt;
{
    public T Data { get; set; }

    [JsonExtensionData]
    public Dictionary&lt;string, object&gt; Metadata { get; set; } = new Dictionary&lt;string, object&gt;();

    public Resource(T data)
    {
        Data = data;

        if (data is ICollection collection)
        {
            Metadata["count"] = collection.Count;
        }
    }
}
I guess you could use inheritance if you really need that extra property:

public class ResourceCollection&lt;TEntity&gt; : Resource&lt;ICollection&lt;TEntity&gt;&gt;
{
    public ResourceCollection(ICollection&lt;TEntity&gt; data) : base(data)
    {
        //modify metadata if needed
    }

    public int Count =&gt; Data.Count;
}


While technically this is still two classes, you can now cast your collections to Resource&lt;T&gt; and process them in generic manner with your other resources.