Question: Working with LINQ i found that when result of the LINQ expression has an interface type it means that it has a deferred execution. Am I right?

**For example:**

```
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };

// Deferred execution because res1 has an IEnumerable<int> type
var res1 = numbers.Where(x => x > 2);

// Immediate execution because res2 has an int[] type
var res2 = numbers.Where(x => x > 2).ToArray();

```

Answer: 
Not correct, even the array implements `IEnumerable<int>`. You don't know from the type if it's using deferred execution since there is no `IDeferred` interface. 

I think the best what you could do is to try to cast it to `ICollection<T>` or `ICollection`:

```
public static bool IsDeferred<T>(this IEnumerable<T> source) {
    if (source == null) throw new ArgumentNullException(nameof(source));
    ICollection<T> genCollection = source as ICollection<T>;
    if (genCollection != null) return false;
    ICollection collection = source as ICollection;
    if (collection != null) return false;
    return true;
}

```

---

```
var arr = new int[5];
bool deferred = arr.IsDeferred(); // false
IEnumerable<int> seq = arr.Where(i => i != 0);
deferred = seq.IsDeferred();      // true

```
That is not right

If you have an IQueryable, that will create an `Expression<Func<T, bool>>` which gives you deferred execution. What you have is an expression.

If you have an IEnumerable, which could be a list, an array or anything else. Or it could even be a function that returns IEnumerable, you end up with a `Func<T, bool>` which is executed as soon as you access it. 

This can be bit of a trap, if you have something like this

```
private IEnumerable<Test> GetTests()
{ 
   return _context.Set<Test>();  
}

```

and you did

```
var tests = GetTests().Take(100);

```

You actually have a `Func<T, bool>` here. You will take all records from db and then just do that `Take(100)` in memory.

If you do

```
var tests = GetTests().AsQueryable().Take(100);

```

now you have an `Expression<Func<T, bool>>` with deferred execution. the `Take(100)` will happen on Db server

Or you could return IQueryable in the first place and not IEnumerable. I am just trying to show the difference between the 2.