I've created the following generic method
protected int GetSpecifiedIndexByName<T>(T collection, string itemName)
{
int count = 0;
foreach (MyElement claim in collection)
{
if (itemName.Equals(claim.Name))
{
return count;
}
count++;
}
}
Which is called by
int index = GetSpecifiedIndexByName<MyCollection>(claimColl, itemName);
However, when I compile this I get the error
Error 1 foreach statement cannot operate on variables of type 'T' because 'T' does not contain a public definition for 'GetEnumerator'
I cannot cast the generic collection to the known collection directly i.e.
MyCollection foo = (MyCollection)collection;
But have to take the intermediary step
Object foo = collection;
MyCollection foobar = (MyCollection)foo;
This is clearly a lack of understanding about generics (and my 1st attempt at it) but any answers relating to why I get the problems (and solution) would be much appreciated.
Thanks
Dhaval