Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SingleOrDefault() method: what is "a default value"?

Tags:

linq

I'm testing for the existence of a user record in the following statement:

if (fromUser.AllFriends.Where(af => af.FriendUserID == toUserID).SingleOrDefault() == ??? 

Given the documentation:

Returns a single, specific element of a sequence, or a default value if that element is not found.

What does the bold text refer to? What the heck am I testing for in my if statement?

A serious question that probably sounds simple and ridiculous to most.

Thanks.

like image 538
asfsadf Avatar asked Sep 14 '10 21:09

asfsadf


People also ask

What is default value SingleOrDefault?

The default value for reference and nullable types is null . The SingleOrDefault method does not provide a way to specify a default value. If you want to specify a default value other than default(TSource) , use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in the Example section.

When should the SingleOrDefault () method be used?

Use Single / SingleOrDefault() when you sure there is only one record present in database or you can say if you querying on database with help of primary key of table. Developer may use First () / FirstOrDefault() anywhere, when they required single value from collection or database.

What is default FirstOrDefault return?

FirstOrDefault<TSource>(IEnumerable<TSource>) Returns the first element of a sequence, or a default value if the sequence contains no elements.

What is single and single or default?

Single : It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection. SingleOrDefault: It returns a single specific element from a collection of elements if element match found.


2 Answers

Excerpts from ECMA bible, verse 334 :


12.2 Default values

The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor (§11.1.1).
  • For a variable of a reference-type, the default value is null.

[Note: Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bitszero to represent the null reference. end note]

The default value of a nullable type is an instance for which the HasValue property is false. Referencing the Value property of a default value of a nullable type results in an exception of type System.InvalidOperationException. The default value is also known as the null value of the nullable type. An implicit conversion exists from the null type (§11.2.7) to any nullable type, and this conversion produces the null value of the type.

18.3.4 Default values

As described in §12.2, several kinds of variables are automatically initialized to their default value when they are created. For variables of class types and other reference types, this default value is null. However, since structs are value types that cannot be null, the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null.

Example: Referring to the Point struct declared above, the example

Point[] a = new Point[100];  

initializes each Point in the array to the value produced by setting the x and y fields to zero.

The default value of a struct corresponds to the value returned by the default constructor of the struct (§11.1.1). Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor, which always returns the value that results from setting all value type fields to their default value and all reference type fields to null.

11.1.2 Default constructors

All value types implicitly declare a public parameterless instance constructor called the default constructor. The default constructor returns a zero-initialized instance known as the default value for the value type:

  • For all simple-types, the default value is the value produced by a bit pattern of all zeros:
    • For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.
    • For char, the default value is '\x0000'.
    • For float, the default value is 0.0f.
    • For double, the default value is 0.0d.
    • For decimal, the default value is 0m.
    • For bool, the default value is false.
  • For an enum-type E, the default value is 0.
  • For a struct-type, the default value is the value produced by setting all value type fields to their default value and all reference type fields to null.
  • For a nullable type, the default value is one for which HasValue returns false.

Amen

You could download the holy book (version 4.0) directly from microsoft website.

like image 77
Julien Roncaglia Avatar answered Sep 28 '22 20:09

Julien Roncaglia


Default value for reference types is null. Default value for numeric types is 0. There are a few other special cases, but if you ever want to be sure, just evaluate default(type) to get the specific default value for any value type you are unsure of. In your specific case, the default value is probably null, assuming you're working with classes.

like image 23
recursive Avatar answered Sep 28 '22 18:09

recursive