FirstOrDefault: Default value other than null

后端 未结 11 1178
-上瘾入骨i
-上瘾入骨i 2020-12-12 21:37

As I understand it, in Linq the method FirstOrDefault() can return a Default value of something other than null. What I haven\'t worked out is wha

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 21:54

    From the documentation for FirstOrDefault

    [Returns] default(TSource) if source is empty;

    From the documentation for default(T):

    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. For nullable value types, default returns a System.Nullable, which is initialized like any struct.

    Therefore, the default value can be null or 0 depending on whether the type is a reference or value type, but you cannot control the default behaviour.

提交回复
热议问题