Question: I'm trying to figure out how to make a python program that highlights the position/positions of a certain inputed word in a sentence and lists what place that word is. For example if the sentence was: "The fat cat sat on the mat"
then the position for the word fat would be Number 2.

Heres what Ive got so far:

```
varSentence = ("The fat cat sat on the mat")

print (varSentence)

varWord = input("Enter word ")

varSplit = varSentence.split()

if varWord in varSplit:
    print ("Found word")
else:
    print ("Word not found")

```

Answer: 
The reason is that the iterator block is always lazy. Unless you call `GetEnumerator()` and then `MoveNext()`, the code in the method won't get executed.

In other words, consider this call to your "equivalent" method:

```
var ignored = OtherEnumerable.Where<string>(null, null);

```

No exception is thrown, because you're not calling `GetEnumerator()` and then `MoveNext()`. Compare that with my version where the exception is thrown *immediately* regardless of how the return value is used... because it only calls the method with the iterator block *after* validating eagerly.

Note that async/await has similar issues - if you have:

```
public async Task FooAsync(string x)
{
    if (x == null)
    {
        throw new ArgumentNullException(nameof(x));
    }
    // Do some stuff including awaiting
}

```

If you call this, you'll end up getting a faulted `Task` - rather than a `NullReferenceException` being thrown. If you await the returned `Task`, *then* the exception will be thrown, but that may not be where you called the method. That's okay in most cases, but worth knowing about.
A method using `yield return` looks very nice and simple, but if you examine the compiled code you will notice it gets quite complicated.

The compiler generates a new class for you with state machine logic to support the enumeration. For the 2nd `Where` method, it's around 160 lines of code after decompilation. The actual `Where` method is compiled to

```
[IteratorStateMachine(typeof(IterarorTest.<Where>d__0<>))]
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    IterarorTest.<Where>d__0<T> expr_07 = new IterarorTest.<Where>d__0<T>(-2);
    expr_07.<>3__source = source;
    expr_07.<>3__predicate = predicate;
    return expr_07;
}

```

As you can see, no arguments are checked in this method. It just returns a new iterator.

The arguments are checked in the auto-generated class' `MoveNext` method (the code is a bit too long to post here).

On the other hand, if you move the `yield return` to another method, the arguments are checked immediately when you call the `Where` method - which is the expected behavior here.

**Edit**

As [noticed by Matias Fidemraizer](https://stackoverflow.com/a/34187364/4588915), code contracts also solve the problem - the contract checks are inserted in the `Where` method

```
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    __ContractsRuntime.Requires(source != null, null, "source != null");
    __ContractsRuntime.Requires(predicate != null, null, "predicate != null");
    IterarorTest.<Where>d__0<T> expr_27 = new IterarorTest.<Where>d__0<T>(-2);
    expr_27.<>3__source = source;
    expr_27.<>3__predicate = predicate;
    return expr_27;
}

```