C# – What does LINQ return when the results are empty

clinq

I have a question about LINQ query. Normally a query returns a IEnumerable<T> type. If the return is empty, not sure if it is null or not. I am not sure if the following ToList() will throw an exception or just a empty List<string> if nothing found in IEnumerable result?

   List<string> list = {"a"};
   // is the result null or something else?
   IEnumerable<string> ilist = from x in list where x == "ABC" select x;
   // Or directly to a list, exception thrown?
   List<string> list1 = (from x in list where x == "ABC" select x).ToList();

I know it is a very simple question, but I don't have VS available for the time being.

Best Answer

It will return an empty enumerable. It won't be null. You can sleep sound :)