Aurrin
November 17th, 2007, 03:19 PM
Okay, I was trying to build up a utility class for efficient joining of collections. To stick two collections together, you could just copy one and then call Append() as necessary, but I read an article about a cleaner way (http://www.c-sharpcorner.com/UploadFile/rmcochran/CartesianProductAndPermutation10082007165346PM/CartesianProductAndPermutation.aspx) to do this and came up with the following:
public static class CollectionUtilities
{
public static IEnumerable<T> JoinCollections<T>( IEnumerable<IEnumerable<T>> collections )
{
foreach ( IEnumerable<T> collection in collections )
{
foreach ( T item in collection )
{
yeild return T;
}
}
}
}
So, this takes a collection of collections, and then tacks them onto one another. I also made an overload that does the same thing but takes params instead, so that an array of collections, or collections seperated by commas can be passed. (A utility should be easy to use, after all.)
And, I dutifully set up some code to test it:
// Set up a list of lists of ints
List<int> FirstList = new List<int>();
FirstList.Add( 1 );
FirstList.Add( 2 );
FirstList.Add( 3 );
List<int> SecondList = new List<int>();
SecondList.Add( 4 );
SecondList.Add( 5 );
SecondList.Add( 6 );
List<List<int>> Lists = new List<List<int>>();
Lists.Add( FirstList );
Lists.Add( SecondList );
// Set up the expected result, to test
List<int> ExpectedResult = new List<int>();
ExpectedResult.Add( 1 );
ExpectedResult.Add( 2 );
ExpectedResult.Add( 3 );
ExpectedResult.Add( 4 );
ExpectedResult.Add( 5 );
ExpectedResult.Add( 6 );
// Run the function
List<int> JoinResult = CollectionUtilities.JoinCollections<int>( Lists );
// Test that the expected result was obtained
Debug.Assert( JoinResult.Equals( ExpectedResult ) );
Except... when try to compile, I get an error:
Error, Argument 1: Could not convert (System.Collections.Generic.List<System.Collections.Generic.List<int>>) to (System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<int>>)
Wha--?!
List<T> implements IEnumerable<T>, so how can it fail a cast to IEnumerable<T>? Especially when the MSDN documentation says that in order to call methods of an explicitly implemented interface, you must first cast to that interface?
(Please ignore small spelling errors, the code is on a different machine, so I had to re-type it.)
public static class CollectionUtilities
{
public static IEnumerable<T> JoinCollections<T>( IEnumerable<IEnumerable<T>> collections )
{
foreach ( IEnumerable<T> collection in collections )
{
foreach ( T item in collection )
{
yeild return T;
}
}
}
}
So, this takes a collection of collections, and then tacks them onto one another. I also made an overload that does the same thing but takes params instead, so that an array of collections, or collections seperated by commas can be passed. (A utility should be easy to use, after all.)
And, I dutifully set up some code to test it:
// Set up a list of lists of ints
List<int> FirstList = new List<int>();
FirstList.Add( 1 );
FirstList.Add( 2 );
FirstList.Add( 3 );
List<int> SecondList = new List<int>();
SecondList.Add( 4 );
SecondList.Add( 5 );
SecondList.Add( 6 );
List<List<int>> Lists = new List<List<int>>();
Lists.Add( FirstList );
Lists.Add( SecondList );
// Set up the expected result, to test
List<int> ExpectedResult = new List<int>();
ExpectedResult.Add( 1 );
ExpectedResult.Add( 2 );
ExpectedResult.Add( 3 );
ExpectedResult.Add( 4 );
ExpectedResult.Add( 5 );
ExpectedResult.Add( 6 );
// Run the function
List<int> JoinResult = CollectionUtilities.JoinCollections<int>( Lists );
// Test that the expected result was obtained
Debug.Assert( JoinResult.Equals( ExpectedResult ) );
Except... when try to compile, I get an error:
Error, Argument 1: Could not convert (System.Collections.Generic.List<System.Collections.Generic.List<int>>) to (System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<int>>)
Wha--?!
List<T> implements IEnumerable<T>, so how can it fail a cast to IEnumerable<T>? Especially when the MSDN documentation says that in order to call methods of an explicitly implemented interface, you must first cast to that interface?
(Please ignore small spelling errors, the code is on a different machine, so I had to re-type it.)