the 3.Times example is pretty cool. I'm curious: what kind of real-world scenario would call for operating on a literal like this
Hi Brian
As a real world example you could take anything where you need a loop. The idea is to avoid an ordinary for next loop with this structure. the 3.Times example shows how a loop could look like in a purely object oriented program if we had the possibility of passing code blocks. A similar idea would be to do something like that for collections:
aCollection.DoForEach(some code)
Currently you can do this with delegates, but the delegate overhead makes it actually longer than an ordinary loop. This delegate overhead will disappear with the C# 3.0 compiler, which will resolve such passed code blocks in anonimous methods.
What about the lambda function in C# Can they be used in order to make the code more like the ruby example By the way, will the lambda function be available in VB, too
Yeah, I understand. But what real world scenario would involve doing an operation on a literal value I'm not picking on the feature, but rather asking a theoretic question. Is the ability to do it against a literal just a "you get it for free anyway" side effect of type extensions or is there a scenario where someone would really want to do this
... like in Ruby
Harrie Nak
Making it possible to extend Int32 answer my question, thanks.
I guess you can yield the values as well to be able to project an int to an enumerable
dhomburg
Hi Brian
As a real world example you could take anything where you need a loop. The idea is to avoid an ordinary for next loop with this structure. the 3.Times example shows how a loop could look like in a purely object oriented program if we had the possibility of passing code blocks. A similar idea would be to do something like that for collections:
aCollection.DoForEach(some code)
Currently you can do this with delegates, but the delegate overhead makes it actually longer than an ordinary loop. This delegate overhead will disappear with the C# 3.0 compiler, which will resolve such passed code blocks in anonimous methods.
Gsundi
Paul
VB Team
Erik Hanson
Keith Newton
What about the lambda function in C# Can they be used in order to make the code more like the ruby example By the way, will the lambda function be available in VB, too
Christian13
Brian
Douglas Lellis
delegate void Procedure();
static class TestExtensions
{
static public void Times(this Int32 subject, Procedure procedure)
{
for (int i = 1; i <= subject; i++)
procedure();
}
}
Which allows you to write:
3.Times(delegate {Console.WriteLine("X");});
Daphne.cheng
static public IEnumerable<Int32> Yield(this Int32 subject)
{
for (int i = 1; i <= subject; i++)
yield return i;
}
and then use 3.Yield() to get an enumerable containing [1, 2, 3].
Lun
the 3.Times example is pretty cool. I'm curious: what kind of real-world scenario would call for operating on a literal like this
Brian