user1771591
user1771591

Reputation: 87

C# (possibly) using Generics

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.

Upvotes: 2

Views: 119

Answers (4)

Mayank
Mayank

Reputation: 8852

public class Response<T>
{
    public int num1 { get; set; }
    public int num2 { get; set; }
    public T[] food { get; set; }
}

Response<Drink> response = new Response<Drink>
    {
        num1= 7, num2= 2, food = new Drink[] 
        { new Drink{Id=1, Name="Orange"}, new Drink{Id=2, Name="Apple"}}
    };

Response<Snack> response = new Response<Snack>
    {
        num1= 7, num2= 2, food = new Snack[] 
        { new Snack{Id=1, Name="Orange"}, new Snack{Id=2, Name="Apple"}}
    };

Upvotes: 0

Dan Puzey
Dan Puzey

Reputation: 34218

As Alex suggested, Generics are the solution to the problem as you describe it. You could redefine your class like this:

public class Response<T>
{ 
    public int num1 { get; set; } 
    public int num2 { get; set; } 
    public T[] items{ get; set; } 
} 

... and then declare your instance like this:

Response<Drink> drinkResponse = new Response<Drink>
{              
    num1= 7, num2= 2, items = new Drink[] { new Drink{Id=1, Name="Orange"}, new Drink{Id=2, Name="Apple"}}  
};  

Response<Snack> snackResponse = new Response<Snack>  
{              
    num1= 7, num2= 2, items = new Snack[] { new Snack{Id=1, Name="Orange"}, new Snack{Id=2, Name="Apple"}}  
};  

Upvotes: 4

Yan Brunet
Yan Brunet

Reputation: 4897

Just Like Alex Farber said, you could create a Generic class.

public class Response<T>
{
    public int num1 { get; set; }
    public int num2 { get; set; }
    public T[] items{ get; set; }
}

More information on generic classes : http://msdn.microsoft.com/en-us/library/sz6zd40f(v=vs.80).aspx

Upvotes: 1

Paul Aldred-Bann
Paul Aldred-Bann

Reputation: 6020

I think you're talking about inheritance here, where you can have multiple classes that share a common structure:

public class BaseResponse
{
    public int num1 { get; set; }
    public int num2 { get; set; }
}

public class DrinkResponse : BaseResponse
{
    public Drink[] drinks { get; set; }
}

public class SnackResponse : BaseResponse
{
    public Snack[] snacks { get; set; }
}

This is an excellent resource for the basics of C#, once you have that down I further recommend you also take a look here for advanced C# 4.0 specific stuff.

Edit

To support the other guys, they make a very good point I didn't think of at first Generics - here is a great article about their usage.

Upvotes: 1

Related Questions