Reputation: 3862
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.
Upvotes: 39
Views: 22509
Reputation: 17837
Excerpts from ECMA bible, verse 334 :
The default value of a variable depends on the type of the variable and is determined as follows:
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.
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.
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:
0
.'\x0000'
.0.0f
.0.0d
.0m
.false
.0
.null
.HasValue
returns false
.Amen
You could download the holy book (version 4.0) directly from microsoft website.
Upvotes: 46
Reputation: 884
ReferenceTypes
default to null
.
ValueTypes
can be found here: http://msdn.microsoft.com/en-us/library/83fhsxwc%28v=VS.90%29.aspx
Upvotes: 2
Reputation: 838876
The default value is the value of default(T) for that type.
But in your case you should use Enumerable.Any
instead:
if (!fromUser.AllFriends.Where(af => af.FriendUserID == toUserID).Any())
{
// etc...
}
You can also combine the Where
and Any
calls:
if (!fromUser.AllFriends.Any(af => af.FriendUserID == toUserID))
{
// etc...
}
Upvotes: 4
Reputation: 56439
a default value is whatever you get when you call default(TypeName)
.
For ReferenceTypes
, this is invariably null
. For ValueTypes
, this is typically 0 (for numeric types) or an instance of the struct after its default constructor is called.
Upvotes: 3
Reputation: 13706
Default value for the element is default(type). For classes default value is null.
Upvotes: 0
Reputation: 86134
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.
Upvotes: 4