Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code:
public IEnumerable<RuleViolation> GetRuleViolations() {
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required","Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
yield return new RuleViolation("Phone# required", "ContactPhone");
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
yield break;
}
I've read up on yield
, but I guess my understanding is still a little bit hazy. What it seems to do is create an object that allows cycling through the items in a collection without actually doing the cycling unless and until it's absolutely necessary.
This example is a little strange to me, though. What I think it's doing is delaying the creation of any RuleViolation
instances until the programmer actually requests a specific item in the collection using either for each
or a LINQ extension method like .ElementAt(2)
.
Beyond this, though, I have some questions:
When do the conditional parts of the
if
statements get evaluated? WhenGetRuleViolations()
is called or when the enumerable is actually iterated? In other words, if the value ofTitle
changes fromnull
toReally Geeky Dinner
between the time that I callGetRuleViolations()
and the time I attempt to actually iterate over it, willRuleViolation("Title required", "Title")
be created or not?Why is
yield break;
necessary? What is it really doing here?Let's say
Title
is null or empty. If I callGetRuleViolations()
then iterate over the resulting enumerable two times in a row, how many times willnew RuleViolation("Title required", "Title")
be called?