What is the default return value of FirstOrDefault? RRS feed

  • Question

  • What is the default return value of FirstOrDefault when nothing is found that satisfies the condition?
    The docs just say "default value of the variable".

    Well, what's the default value of a Reference Type? Null?
    What's the default value of an int? 0?
    What's the default value of a nullable type like "long?" Null?

    Forgive if wrong LINQ forums. Please redirect if that's the case. It is very hard to tell which LINQ forum to ask in when you are new to LINQ.

    Monday, February 15, 2010 9:59 PM


All replies

  • Hi Christopher,


    If the source is empty, the method FirstOrDefault() will retrieve the default(TSource).  Please refer to the following sample codes:


                List<long> longList = new List<long>();

                long firstLong = longList.FirstOrDefault();

                long l = default(long);           


    The variable firstLong equals default(long) = 0.   And same for other types like int, string and custom classes.


                int i = default(int); 0

                string s = default(string); null

                MyClass mc = default(MyClass); null



    For general LINQ to Objects issues, I would recommend post at LINQ Project General forum. 

    For LINQ to SQL, the forum queue here,

    For Entity Framework,

    For LINQ to DataSet,  


    Have a nice day!



    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, February 16, 2010 1:58 AM
  • In generic classes and methods,given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.
    Thanks, B.C.
    Wednesday, June 30, 2010 7:50 AM