C# yield operator with argument validation
-
IEnumerable<int> GetEvenIntegers(IEnumerable<int> integers)
{
if(integers == null)
{
throw new ArgumentNullException("The integers parameter cannot be null.");
}foreach(int i in integers)
{
if(i % 2 == 0)
{
yield return i;
}
}
}// What exception does this throw?
IEnumerable<int> evenNumbers = GetEvenIntegers(null);Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
IEnumerable<int> GetEvenIntegers(IEnumerable<int> integers)
{
if(integers == null)
{
throw new ArgumentNullException("The integers parameter cannot be null.");
}foreach(int i in integers)
{
if(i % 2 == 0)
{
yield return i;
}
}
}// What exception does this throw?
IEnumerable<int> evenNumbers = GetEvenIntegers(null);Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Oh, you beat me then. Dang. :)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Nothing. http://www.codeproject.com/Feature/SubtleBugs.asp?msg=1683788#xx1683788xx[^];)
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
that indeed is a subtle bug, .. how does it throw nothing?
-
that indeed is a subtle bug, .. how does it throw nothing?
Because until you call on GetEnumerator on the returned enumerator the code within the function does not get executed.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
that indeed is a subtle bug, .. how does it throw nothing?
Ian MacLean wrote:
how does it throw nothing
This is something borrowed from functional programming -- the delayed execution part, not the no-exception throw. Under the hood, the C# compiler generates a class that implements IEnumerator, containing all captured local variables. Each time foreach is iterated (which corresponds to IEnumerator.MoveNext() method call), the next line would be yielded. Because of this delayed execution, you can pull of some pretty amazing things, such as yielding every even integer without having to allocate an array of size int.Maximum, or yield recursive hierarchies without having to allocate lists, and so on. But it does introduce subtle bugs like the one above.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango