
.NET iterator to wrap throwing API
By: a guest on Feb 23rd, 2012 | syntax:
None | size: 1.99 KB | hits: 9 | expires: Never
static IEnumerable<object> Iterator( ExAPI api ) {
try {
for( int i = 0; true; ++i ) {
yield return api[i]; // will throw eventually
}
}
catch( IndexOutOfBoundsException ) {
// expected: end of iteration.
}
}
static IEnumerable<object> Iterator( ExAPI api ) {
for( int i = 0; true; ++i ) {
object current;
try {
current = api[i];
} catch(IndexOutOfBoundsException) { yield break; }
yield return current;
}
}
bool TryGetObject( ExAPI api, int idx, out object obj )
{
try
{
obj = api[idx];
return true;
}
catch( IndexOutOfBoundsException )
{
return false;
}
}
static IEnumerable<object> Iterator( ExAPI api )
{
bool abort = false;
for( int i = 0; !abort; ++i )
{
object obj;
if( TryGetObject( api, i, out obj ) )
{
yield return obj;
}
else
{
abort = true;
}
}
}
static IEnumerable<object> Iterator( ExAPI api ) {
bool exceptionThrown = false;
object obj = null;
for( int i = 0; true; ++i ) {
try {
obj = api[i];
}
catch( IndexOutOfBoundsException ) {
exceptionThrown = true;
yield break;
}
if (!exceptionThrown) {
yield return obj;
}
}
}
static IEnumerable<object> Iterator( ExAPI api )
{
List<object> output = new List<object>();
try
{
for( int i = 0; true; ++i )
output.Add(api[i]);
}
catch( IndexOutOfBoundsException )
{
// expected: end of iteration.
}
return output;
}
static IEnumerable<object> Iterator(ExAPI api)
{
int i = 0;
while (true)
{
Object a;
try
{
a = api[i++];
}
catch (IndexOutOfBoundsException)
{
yield break;
}
yield return a;
}
}