3

While perusing Twitter I spotted a tweet by a Game Developer I follow that just said;

@ChevyRay 2:44 AM - 5 Jul 2016
i give you: the stupidest 8 lines of code i’ve ever written that is actually used by my game’s code

static IEnumerable<int> RightAndleft
{
    get
    {
        yield return 1;
        yield return -1;
    }
}

Immediately I looked at it and thought to myself what does this do? I'm sure the post was meant as an in-joke but I just didn't get it. So with that I went and researched yield but that didn't really answer my question.

I wondered if anyone here could shed some light on firstly what it's doing and also why.

15
  • It returns 2 ints, 1 and -1 for left and right .. Commented Jul 5, 2016 at 10:09
  • Appreciate the quick reply @BugFinder but that is what I'm trying to understand, how can a property return two values, is that what yield allows you to do? I guess I just don't get the syntax? Commented Jul 5, 2016 at 10:14
  • 2
    There's a lot of compiler magic going on here. See what the compiler turns this into: it creates an inner class (a state-machine) and RightAndLeft returns an instance of this class. tryroslyn.azurewebsites.net/… Commented Jul 5, 2016 at 10:22
  • 1
    @Lankymart It's a lot of syntactic sugar. This guy always explains things very well: youtube.com/… Commented Jul 5, 2016 at 10:25
  • 1
    yep. Yield is in a way quite a brain changer for those of us who coded in the days before it, however.. it is a lovely way of handling it Commented Jul 5, 2016 at 12:27

2 Answers 2

4

The compiler turns this code into an enumerator. You can use this enumerator to iterate over the sequence {1, -1}:

foreach(var i in ClassName.RightAndLeft)
    Console.WriteLine(i);

results in

1
-1

Note that this property does not have two return statements and return type int. It returns an IEnumerable<int>, a sequence containing 1 and -1.

See yield keyword for more information.

One important part to note is that the second line yield return -1; is executed after the first value has been printed out by Console.WriteLine in this example.


Since the poster is a game developer, he probably uses these two values as direction indicators and this enumeration to, well, enumerate all possible directions or something like that.

Sign up to request clarification or add additional context in comments.

4 Comments

How can a property have two return statements, is that what yield allows you to do?
@Lankymart yield tells the compiler to build an enumerator out of this code. The return type of the property is not int, it's IEnumerable<int>. I added a link to an explanation of yield works.
Thanks @RenéVogt I'm a lot clearer on what it is doing now.
Weird, someone decided to downvote both the question and your answer on Jun 12th at 17:00. Noticed the question had been flagged for closure but it was invalidated. Honestly i've seen far worse questions on Stack Overflow over the years. Didn't downvote the other answer though...you get on the wrong side of someone? ;)
1

It returns IEnumerable<int> object that contains 1 and -1.

See https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx

1 Comment

It doesn't really contain them. It can produce them when asked, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.