Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get the feeling the answer to this must either be obvious or so obscure I need to find a better way, but I have been trying to include a generic type in a Tuple. The following code, which does not compile, shows the idea of what I am trying to do:

List<Tuple<string,List<T>>> listOfTuples = null;

void InitialiseList()
{
    listOfTuples = new ...
    listOfTuples.Add( new ... );
    ...
}


What I have tried:

I've tried a fair bit of Googling, but everything I have found so far seems to be about creating generic types to hold tuples, not tuples to hold generic types.

I could of course just create a dedicated class or struct to hold the elements of the list, but I wondered if there was an easier way.

By the way, I am on C#6.
Posted
Updated 12-Sep-17 1:19am
v2

I think that this is what you are trying to do:
C#
Tuple<string, List<T>> TupleFactory<T>(string value, List<T> values)
{
    return new Tuple<string, List<T>>(value, values);
}

To use:
C#
var listOfTuples1 = TupleFactory("test value", new List<string>
    {
        "test value 1",
        "test value 2",
        "test value 3",
        "test value 4",
    });

var listOfTuples2 = TupleFactory("test value", new List<int> { 1, 2, 3, 4 });
 
Share this answer
 
v2
You could try using inheritance but Solution 1 is probably better if it can be used

public class MyData<T> : List<Tuple<string, List<T>>>
{

}


MyData<string> dataA = new MyData<string>();

dataA.Add(new Tuple<string, List<string>>("String 1", new List<string> { "A", "B", "C" }));
dataA.Add(new Tuple<string, List<string>>("String 2", new List<string> { "X", "Y", "Z" }));


MyData<int> dataB = new MyData<int>();

dataB.Add(new Tuple<string, List<int>>("Int 1", new List<int> { 1, 2, 3 }));
dataB.Add(new Tuple<string, List<int>>("Int 2", new List<int> { 4, 5, 6 }));
 
Share this answer
 
Comments
Patrick Skelton 11-Sep-17 14:05pm    
Actually, I think this highlights where my thinking is muddy. I can't use this code because each tuple I add has a different type parameter. That said, all the types do share a common base class, but I still can't persuade the compiler to see my side of things.
F-ES Sitecore 11-Sep-17 14:42pm    
Something like

public abstract class Animal
{
public string Name { get; set; }
}

public class Cat : Animal
{
}

public class Dog : Animal
{
}

List<Tuple<string, List<animal>>> x = new List<Tuple<string, List<animal>>>();

List<animal> cats = new List<animal>();
cats.Add(new Cat { Name = "Tiddles" });

List<animal> dogs = new List<animal>();
dogs.Add(new Dog { Name = "Spot" });

x.Add(new Tuple<string, List<animal>>("List 1", cats));
x.Add(new Tuple<string, List<animal>>("List 2", dogs));
Patrick Skelton 12-Sep-17 7:43am    
This is weird. I did have something close to your code, though I have created a dedicated private class to hold the items instead of using tuples. I am obviously doing something different because I get a compiler error where I try to add the item to the dictionary saying that the derived class cannot be converted to the base class.

private class BusinessObjectCacheItem<t> where T : BusinessObjectBase
{
public BusinessObjectCacheItem( List<t> businessObjectList, DataCommunicator<t> dataCommunicator )
{
_businessObjectList = businessObjectList;
_dataCommunicator = dataCommunicator;
}

public List<t> BusinessObjectList { get { return _businessObjectList; } }
public DataCommunicator<t> DataCommunicator { get { return _dataCommunicator; } }

private List<t> _businessObjectList = null;
private DataCommunicator<t> _dataCommunicator = null;

}

private Dictionary<BusinessObjectName,BusinessObjectCacheItem<businessobjectbase>> _cacheItems = null;

private void InitialiseCacheItems()
{
_cacheItems = new Dictionary<BusinessObjectName,BusinessObjectCacheItem<businessobjectbase>>();
_cacheItems.Add( BusinessObjectName.Contact, new BusinessObjectCacheItem<businessobjectbase>( _contactMasterList, _contactDataReaderWriter ) );
_cacheItems.Add( BusinessObjectName.Contact, new BusinessObjectCacheItem<contact>( _contactMasterList, _contactDataReaderWriter ) );
// Neither of the last two lines will compile even though Contact is derived from BusinessObjectBase.
}
I'm wondering why no one said the obvious - Why you mess arround with tuples for such cases? They are not for "complicated cases" just create a type to hold your information and make a list of that....
just my 2c
 
Share this answer
 
Comments
Patrick Skelton 12-Sep-17 7:22am    
I did mention in my question that this might be the way to go. The code would be more readable.

Thanks for the reply.
johannesnestler 12-Sep-17 7:37am    
sry, it seems i read over it. so all good ;)
Graeme_Grant 12-Sep-17 7:37am    
That was not the question, but yes, Tuples are not my first choice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900