Translated fromJon SkeetSeries of blog posts"Edulinq".
Original article address:
Http://msmvps.com/blogs/jon_skeet/archive/2010/12/24/reimplementing-linq-to-objects-part-6-repeat.aspx
The topic of this article is irrelevant,Repeat. AboutRepeat.EmptyLess. Write this blog to ensure the integrity of this series.
RepeatWhat is it?
RepeatIt is a static generic method, not an extension method. It has only one signature form:
Public StaticIenumerable <tresult> repeat <tresult> (
Tresult element,
IntCount)
It returns a sequence that contains"Count,.RepeatOnly one parameter verification is required: Check"Count"Is not a negative number.
What should we test?
There are not many things to test. I only want to use four scenarios:
LA simple test repeats a string three times
LAn empty sequence (repeat one element0Times)
LMultiple containNullSequence (just to prove"Element"Can beNull)
LUse a negative number asCountTo prove that the parameter verification will be executed and is executed immediately.
I am afraid these points are not very popular.
To implement it!
The only thing we may do wrong during implementation is to verify the parameters.CodeIt is written together with the iterator code block. However, we have already done "Split implementation" for many times, so we will certainly not make this mistake. The following code is not good enough.RepeatThe methods are all:
Public StaticIenumerable <tresult> repeat <tresult> (tresult element,IntCount)
{
If(Count <0)
{
ThrowNewArgumentoutofrangeexception ("Count");
}
ReturnRepeatimpl (element, count );
}
Private StaticIenumerable <tresult> repeatimpl <tresult> (tresult element,IntCount)
{
for ( int I = 0 ; I
{
YieldReturnElement;
}
}
This is all about today. It is worth noting that...There is nothing worth noting.
Conclusion
In fact, there is no conclusion to write. Next time we will talk aboutCountAndLongcountThere are some interesting things to elaborate on.