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? When GetRuleViolations()
is called or when the enumerable is actually iterated? In other words, if the value of Title
changes from null
to Really Geeky Dinner
between the time that I call GetRuleViolations()
and the time I attempt to actually iterate over it, will RuleViolation("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 call GetRuleViolations()
then iterate over the resulting enumerable two times in a row, how many times will new RuleViolation("Title required", "Title")
be called?
C is the only programming language that exists for such a long period and still it is widely used. C is the basis of many other programming languages like C++, Java, JavaScript, Go, C#, PHP, Python, Perl, C-shell and many more.
C programming language is majorly used in the creation of hardware devices, operating systems, drivers, kernels, etc. It is also used for the development of GUIs and IDEs. For example: Linux Kernel is written in the C language.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
A function that contains yield
commands is treated differently than a normal function. What is happening behind the scenes when that function is called, is that an anonymous type is constructed of the specific IEnumerable
type of the function, the function creates an object of that type and returns it. The anonymous class contains logic that executes the body of the function up until the next yield
command for every time the IEnumerable.MoveNext
is called. It is a bit misleading, the body of the function is not executed in one batch like a normal function, but rather in pieces, each piece executes when the enumerator moves one step forward.
With regards to your questions:
if
gets executed when you iterate to the next element.yield break
is indeed not necessary in the example above. What it does is it terminates the enumeration.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With