4

In MSDN help page on Enumerable.FirstOrDefault Method there is a method result explained as:

default(TSource) if source is empty; otherwise, the first element in source.

Remarks section contains note:

The default value for reference and nullable types is null.

I was always doing check for null (VB.NET: Nothing) value but is there some default(TSource) which can be used instead of null/Nothing literal? (For example default(int).)

I cannot find default(TSource) method, but it is mentioned on help page. Or isn't it a method?

EDIT: default(TSource) is visible on MSDN page for both C# and VB and I'm interested in answer covering both languages.

miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • 1
    [default](http://msdn.microsoft.com/en-us/library/25tdedf5.aspx) - not a function or method – Grundy Dec 17 '14 at 12:27

3 Answers3

4

You can use the default operator, which is documented here. It returns null for reference types, and a type-dependent default value for value types.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Codor
  • 17,447
  • 9
  • 29
  • 56
3

default is the keyword used to get the default value of Type. TSource is the generic type parameter. It is just the place holder for type token.

When the method is invoked with IEnumerable<int>, TSource will be int, TSource will be String when invoked with IEnumerable<String> and so on. Given that default(TSource) becomes default(int) in this case (invoked with int).

There is nothing like default(TSource) unless there exist a type TSource or a generic parameter exist with name TSource.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • OK. To add VB.NET equivalent, I'm appending [this link](http://stackoverflow.com/questions/354136/default-value-for-generics). – miroxlav Dec 17 '14 at 12:42
2

I was always doing check for null (VB.NET: Nothing)

This not quite correct. Nothing is not the same as null; Nothing means null when assigned or compared to a nullable type (reference type or Nullable<T>) with = or when compared using Is Nothing and means the default value for a non-nullable value type it is assigned to or compared with =.

Hence the VB:

Dim b as Boolean = 0 = Nothing ' b is True

is not the same as the C#:

bool b = 0 == null; // b is false

but rather of:

bool b = 0 == default(int); // b is true

So the VB.NET equivalent of default(T) is indeed Nothing when not compared using Is.

In VB.NET you are not allowed to do val Is Nothing with val is not nullable, while in C# you can do val == null but it causes a warning (and always results in false).

In VB.NET you are allowed to do val Is Nothing with a generic type that could be nullable, and likewise with C# and val == null, in which case the check is that val is a nullable type and that it is set to null (and a waste-free one at that, generally in the case of a non-nullable type the jitter optimises away anything that would happen if val == null/val Is Nothing since it knows that can never happen).

The following VB.NET and C# methods are equivalent:

public static bool Demonstrate<T>(T x)
{
  T y = default(T);
  bool isNull = x == null;
  bool isDefault = x.Equals(default(T));
  int zero = default(int)
  return zero == default(int);
}

Public Shared Function Demonstrate(Of T)(x As T) As Boolean
  Dim y As T = Nothing
  Dim isNull As Boolean = x Is Nothing
  Dim isDefault As Boolean = x.Equals(Nothing)
  Dim zero As Integer = Nothing
  Return zero = Nothing
End Function
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251