How to specify a class that may contain another class

I want to create a class that has a class, but this second class may be different each time the first class is called. For example:

public class ServerResponseObject
{
    public string statusCode { get; set; }
    public string errorCode { get; set; }
    public string errorDescription { get; set; }
    public Object obj { get; set; }

    public ServerResponseObject(Object obje)
    {
        obj = obje;
    }
}   

public class TVChannelObject
{
    public string number { get; set; }
    public string title { get; set; }
    public string FavoriteChannel { get; set; }
    public string description { get; set; }
    public string packageid { get; set; }
    public string format { get; set; }
}

public class ChannelCategoryObject
{
    public string id { get; set; }
    public string name { get; set; }
}

How to do this to call ServerResponseObjectwith different objects each time , once with TVChannelObjectand once with ChannelCategoryObject?

+4
source share
1 answer

What you are looking for is a generic parameter type :

public class ServerResponseObject<T>
{
    public ServerResponseObject(T obj)
    {
        Obj = obj;
    }

    public T Obj { get; set; }
}
+11
source

Source: https://habr.com/ru/post/1608290/


All Articles