I’m pretty new to this, so please bear with me.
I have a class which has three properties: a couple of ints and a collection of userdefined objects.
public class Response
{
public int num1 { get; set; }
public int num2 { get; set; }
public Drink[] drinks{ get; set; }
}
I can instantiate the class using the userdefined objects and everything works great.
Response response = new Response
{
num1= 7, num2= 2, drinks= new Drink[]
{ new Drink{Id=1, Name="Orange"}, new Drink{Id=2, Name="Apple"}}
};
How can I make that third property of the Response class to where I can have another instance of the class using a different userdefined object, say, “Snack”.
Looking for an online resource to read/learn/figure out more so than an answer, although either would be greatly appreciated.
As Alex suggested, Generics are the solution to the problem as you describe it. You could redefine your class like this:
… and then declare your instance like this: